Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is line-joining unsupported by f-strings?

Is the following syntax not supported by f-strings in Python 3.6? If I line join my f-string, the substitution does not occur:

SUB_MSG = "This is the original message."

MAIN_MSG = f"This longer message is intended to contain " \
             "the sub-message here: {SUB_MSG}"

print(MAIN_MSG)

returns:

This longer message is intended to contain the sub-message here: {SUB_MSG}

If I remove the line-join:

SUB_MSG = "This is the original message."

MAIN_MSG = f"This longer message is intended to contain the sub-message here: {SUB_MSG}"

print(MAIN_MSG)

it works as expected:

This longer message is intended to contain the sub-message here: This is the original message.

In PEP 498, backslashes within an f-string are expressly not supported:

Escape sequences

Backslashes may not appear inside the expression portions of f-strings, so you cannot use them, for example, to escape quotes inside f-strings:

>>> f'{\'quoted string\'}'

Are line-joins considered 'inside the expression portion of f-strings' and are thus not supported?

like image 282
JS. Avatar asked Oct 24 '17 22:10

JS.


People also ask

Can you use \n in F-strings?

As shown in the above code, instead of single or double quotes, we use triple quotes to enclose the multiline string. At the same time, we can still use the newline ( \n ) in the f-string to signal that there is a new line in the string.

How do I add a new line to an F-string?

Essentially, you have three options; The first is to define a new line as a string variable and reference that variable in f-string curly braces. The second workaround is to use os. linesep that returns the new line character and the final approach is to use chr(10) that corresponds to the Unicode new line character.

What is the purpose of an F-string?

F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with 'f', which contains expressions inside braces.

Why are F-strings better?

As of Python 3.6, f-strings are a great new way to format strings. Not only are they more readable, more concise, and less prone to error than other ways of formatting, but they are also faster!


2 Answers

You have to mark both strings as f-strings to make it work, otherwise the second one is interpreted as normal string:

SUB_MSG = "This is the original message."

MAIN_MSG = f"test " \
           f"{SUB_MSG}"

print(MAIN_MSG)

Well, in this case you could also just make the second string the f-string because the first one doesn't contain anything to interpolate:

MAIN_MSG = "test " \
           f"{SUB_MSG}"

Note that this affects all string-prefixes not just f-strings:

a = r"\n" \
     "\n"
a   # '\\n\n'   <- only the first one was interpreted as raw string

a = b"\n" \
     "\n"   
# SyntaxError: cannot mix bytes and nonbytes literals
like image 134
MSeifert Avatar answered Sep 28 '22 02:09

MSeifert


Try this (note the extra “f” on the continuation line):

SUB_MSG = "This is the original message."

# f strings must be aligned to comply with PEP and pass linting
MAIN_MSG = f"This longer message is intended to contain " \
           f"the sub-message here: {SUB_MSG}"


print(MAIN_MSG)
like image 36
Lawrence D'Oliveiro Avatar answered Sep 28 '22 01:09

Lawrence D'Oliveiro