How to use newline '\n' in f-string to format output in Python 3.6?

PythonPython 3.xNewlinePython 3.6F String

Python Problem Overview


I would like to know how to format this case in a Pythonic way with f-strings:

names = ['Adam', 'Bob', 'Cyril']
text = f"Winners are:\n{'\n'.join(names)}"
print(text)

The problem is that '\' cannot be used inside the {...} expression portions of an f-string. Expected output:

Winners are:
Adam
Bob
Cyril

Python Solutions


Solution 1 - Python

You can't. Backslashes cannot appear inside the curly braces {}; doing so results in a SyntaxError:

>>> f'{\}'
SyntaxError: f-string expression part cannot include a backslash

This is specified in the PEP for f-strings:

> Backslashes may not appear inside the expression portions of f-strings, [...]

One option is assinging '\n' to a name and then .join on that inside the f-string; that is, without using a literal:

names = ['Adam', 'Bob', 'Cyril']
nl = '\n'
text = f"Winners are:{nl}{nl.join(names)}"
print(text)

Results in:

Winners are:
Adam
Bob
Cyril

Another option, as specified by @wim, is to use chr(10) to get \n returned and then join there. f"Winners are:\n{chr(10).join(names)}"

Yet another, of course, is to '\n'.join beforehand and then add the name accordingly:

n = "\n".join(names)
text = f"Winners are:\n{n}"

which results in the same output.

Note:

This is one of the small differences between f-strings and str.format. In the latter, you can always use punctuation granted that a corresponding wacky dict is unpacked that contains those keys:

>>> "{\\} {*}".format(**{"\\": 'Hello', "*": 'World!'})
"Hello World!"

(Please don't do this.)

In the former, punctuation isn't allowed because you can't have identifiers that use them.


Aside: I would definitely opt for print or format, as the other answers suggest as an alternative. The options I've given only apply if you must for some reason use f-strings.

Just because something is new, doesn't mean you should try and do everything with it ;-)

Solution 2 - Python

You don't need f-strings or other formatters to print a list of strings with a separator. Just use the sep keyword argument to print():

names = ['Adam', 'Bob', 'Cyril']
print('Winners are:', *names, sep='\n')

Output:

Winners are:
Adam
Bob
Cyril

That said, using str.join()/str.format() here would arguably be simpler and more readable than any f-string workaround:

print('\n'.join(['Winners are:', *names]))
print('Winners are:\n{}'.format('\n'.join(names)))

Solution 3 - Python

You can't use backslashes in f-strings as others have said, but you could step around this using os.linesep (although note this won't be \n on all platforms, and is not recommended unless reading/writing binary files; see Rick's comments):

>>> import os
>>> names = ['Adam', 'Bob', 'Cyril']
>>> print(f"Winners are:\n{os.linesep.join(names)}")
Winners are:
Adam
Bob
Cyril 

Or perhaps in a less readable way, but guaranteed to be \n, with chr():

>>> print(f"Winners are:\n{chr(10).join(names)}")
Winners are:
Adam
Bob
Cyril

Solution 4 - Python

The other answers give ideas for how to put the newline character into a f-string field. However, I would argue that for the example the OP gave (which may or may not be indicative of OP's actual use case), none of these ideas should actually be used.

The entire point of using f-strings is increasing code readability. There is nothing you can do with f-strings that you cannot do with format. Consider carefully whether there is anything more readable about this (if you could do it):

f"Winners are:\n{'\n'.join(names)}"

...or this:

newline = '\n'
f"Winners are:\n{newline.join(names)}"

...or this:

"Winners are:\n{chr(10).join(names)}"

vs. this:

"Winners are:\n{}".format('\n'.join(names))

The last way is at least as readable, if not more so.

In short: don't use a hammer when you need a screwdriver just because you have a shiny new one. Code is read much more often than it is written.

For other use cases, yes, it's possible the chr(10) idea or newline idea may be appropriate. But not for the one given.

Solution 5 - Python

print(f'{"blah\n"}')

The above statement will raise SyntaxError, But to avoid the error, you can simply assign the string containing \n to a variable and use it in f-string.

x = "blah\n"
print(f'{x}')

Solution 6 - Python

If (and only if!) readability is the top priority, and speed is truly not a factor, f-strings are very useful to make a simple function self documenting even if there are simpler ways to program it. Readability is maximized with f-strings when: (1) the statements that change the argument's state are clearly obvious, and (2) when the arguments are printed, the print statement is carefully formatted, and visually presented to make the arguments stand out:

'''
function to print out an identification header, consisting
of the programmer supplied title, lastname, and firstname:
''' 
FORMAT_DATE = "%m-%d-%y %H:%M:%S"

date_and_time = datetime.now()
name_line = f"* {lastname}, {firstname}"
title_line = f"* {title}"
date_line = f"* {date_and_time.strftime(FORMAT_DATE)}"
print(name_line
    + '\n'
    + title_line
    + '\n'
    + date_line)

output:

* Lovelace, Ada
* Bernoulli Numbers algorithm implemented in Python
* 10-28-42 20:13:22

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmalmedView Question on Stackoverflow
Solution 1 - PythonDimitris Fasarakis HilliardView Answer on Stackoverflow
Solution 2 - PythonEugene YarmashView Answer on Stackoverflow
Solution 3 - PythonChris_RandsView Answer on Stackoverflow
Solution 4 - PythonRick supports MonicaView Answer on Stackoverflow
Solution 5 - PythonCrackerKSRView Answer on Stackoverflow
Solution 6 - PythonBill StrattonView Answer on Stackoverflow