How to Add New Lines in Python f-strings  | Towards Data Science

In Python, it’s impossible to include backslashes in curly braces {} of f-strings. Doing so will result into a SyntaxError:
>>> f'{\}'SyntaxError: f-string expression part cannot include a backslash
This behaviour aligns perfectly with PEP-0498 which is about Literal String Interpolation:
Backslashes may not appear inside the expression portions of f-strings, so you cannot use them, for example, to escape quotes inside f-strings
In the following sections we will explore a couple of options you can use in order to add backslashes (including new lines) in f-strings.
As we already discussed, backslashes cannot be used directly in Python f-strings. However, there is an easy workaround that allows us to include backslashes in order to denote for example new lines, tabs etc.
Let’s suppose that we have a list of integers we want to print out to the standard output.
nums = [10, 20, 30] print(f"Numbers:\n {'\n'.join(map(str, nums))}")
As expected, the above will fail with
SyntaxError: f-string expression part cannot include a backslash
One workaround is to add the new line character (i.e. '\n') into a string variable and then reference that variable when printing out the numbers. For example,
new_line = '\n'nums = [10, 20, 30] print(f"Numbers:{new_line}{new_line.join(map(str, nums))}")
And the output should be
Numbers:102030
Note that you can do this for pretty much every character that requires a backslash. For instance, let’s suppose you want to use a tab in an f-string. The following should to the trick:
tab = '\t'print(f'Hello {tab} World!')# OutputHello   World!
For new lines in particular, there is also another approach that we can follow in order to add new line characters in Python f-strings.
The os package comes with some functionality about Miscellaneous System Information. This includes os.linesep that returns the new line character.
The string used to separate (or, rather, terminate) lines on the current platform. This may be a single character, such as '\n' for POSIX, or multiple characters, for example, '\r\n' for Windows.
For example,
import osnums = [10, 20, 30] print(f"Numbers:{os.linesep}{os.linesep.join(map(str, nums))}")
And the output will again be
Numbers:102030
Note that you must avoid using os.linesep as a line terminator when writing files opened in text mode. Instead, you have to use use a single '\n'.
Another possibility, is to use chr() function to generate a new line character. The chr() function in Python returns a string representation of the input integer that corresponds to a Unicode character. The decimal value of 10 in Unicode is equivalent to the line feed (i.e. new line).
>>> chr(10)'\n'
Therefore, in order to add new lines in f-strings using chr() all you need to do is
nums = [10, 20, 30] print(f"Numbers:{chr(10)}{chr(10).join(map(str, nums))}")# Numbers:# 10# 20# 30
In today’s article we discussed one limitation of Python that stops us from using backslashes in f-strings. We introduced a few possible workarounds and we also explored in particular how to implicitly add new lines in Python f-strings.
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.