Questions tagged [f-string]

Literal string interpolation in Python

An f-string is the abbreviated name for a "formatted string" syntax that was introduced in Python 3.6 and extended in Python 3.7. They enable literal string interpolation:

>>> value = 80
>>> f'The value is {value}.'
'The value is 80.'

See PEP 498 for a comprehensive description of the feature, and also PEP 536 for the final grammar.

621 questions
1448
votes
16 answers

String formatting: % vs. .format vs. f-string literal

There are various string formatting methods: Python <2.6: "Hello %s" % name Python 2.6+: "Hello {}".format(name)   (uses str.format) Python 3.6+: f"{name}"   (uses f-strings) Which is better, and for what situations? The following methods have…
NorthIsUp
  • 17,502
  • 9
  • 29
  • 35
891
votes
8 answers

Fixed digits after decimal with f-strings

Is there an easy way with Python f-strings to fix the number of digits after the decimal point? (Specifically f-strings, not other string formatting options like .format or %) For example, let's say I want to display 2 digits after the decimal…
GafferMan2112
  • 9,057
  • 3
  • 11
  • 11
292
votes
1 answer

How to escape curly-brackets in f-strings?

I have a string in which I would like curly-brackets, but also take advantage of the f-strings feature. Is there some syntax that works for this? Here are two ways it does not work. I would like to include the literal text {bar} as part of the…
JDAnders
  • 4,831
  • 4
  • 10
  • 8
265
votes
5 answers

Multiline f-string in Python

I'm trying to write PEP-8 compliant code for a domestic project and I have a line with an f-string that is more than 80 characters long – the solid thin line near the dot at self.text is the 80 char mark. I'm trying to split it into different lines…
Owlzy
  • 3,352
  • 2
  • 12
  • 12
239
votes
8 answers

How can I use newline '\n' in an f-string to format output?

I tried this code: names = ['Adam', 'Bob', 'Cyril'] text = f"Winners are:\n{'\n'.join(names)}" print(text) However, '\' cannot be used inside the {...} expression portions of an f-string. How can I make it work? The result should be: Winners…
malmed
  • 2,698
  • 2
  • 13
  • 11
179
votes
15 answers

How to postpone/defer the evaluation of f-strings?

I am using template strings to generate some files and I love the conciseness of the new f-strings for this purpose, for reducing my previous template code from something like this: template_a = "The current name is {name}" names = ["foo",…
JDAnders
  • 4,831
  • 4
  • 10
  • 8
127
votes
7 answers

What is print(f"...")

I am reading through a python script that takes an input of XML files and outputs an XML file. However, I do not understand the printing syntax. Can someone please explain what f in print(f"...") does? args = parser.parser_args() print(f"Input…
user11760831
125
votes
1 answer

In Python format (f-string) strings, what does !r mean?

I get what the new f strings in python 3.6 do, but what about the ending !r as found in the code snip below. def __repr__(self): return (f'Pizza({self.radius!r}, 'f'{self.ingredients!r})')
nk abram
  • 1,531
  • 2
  • 11
  • 19
113
votes
6 answers

Combine f-string and raw string literal

I'm wondering how to use an f-string whilst using r to get a raw string literal. I currently have it as below but would like the option of allowing any name to replace Alex I was thinking adding an f-string and then replacing Alex with curly braces…
lammyalex
  • 1,179
  • 2
  • 7
  • 8
111
votes
1 answer

Why is f'{{{74}}}' the same as f'{{74}}' with f-Strings?

f-Strings are available from Python 3.6 and are very useful for formatting strings: >>> n='you' >>> f'hello {n}, how are you?' 'hello you, how are you?' Reading more about them in Python 3's f-Strings: An Improved String Formatting Syntax (Guide).…
fedorqui
  • 275,237
  • 103
  • 548
  • 598
97
votes
2 answers

How can I split up a long f-string in Python?

I am getting a line too long PEP 8 E501 issue. f'Leave Request created successfully. Approvers sent the request for approval: {leave_approver_list}' I tried using a multi-line string, but that brings in a \n, which breaks my test: f'''Leave Request…
tread
  • 10,133
  • 17
  • 95
  • 170
92
votes
5 answers

f-strings vs str.format()

I'm using the .format() a lot in my Python 3.5 projects, but I'm afraid that it will be deprecated during the next Python versions because of f-strings, the new kind of string literal. >>> name = "Test" >>> f"My app name is {name}." 'My app name is…
90
votes
15 answers

Nested f-strings

Thanks to David Beazley's tweet, I've recently found out that the new Python 3.6 f-strings can also be nested: >>> price = 478.23 >>> f"{f'${price:0.2f}':*>20s}" '*************$478.23' Or: >>> x = 42 >>>…
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
84
votes
6 answers

Is there a formatted byte string literal in Python 3.6+?

I'm looking for a formatted byte string literal. Specifically, something equivalent to name = "Hello" bytes(f"Some format string {name}") Possibly something like fb"Some format string {name}". Does such a thing exist?
Enrico Borba
  • 1,877
  • 2
  • 14
  • 26
84
votes
7 answers

How can I do a dictionary format with f-string in Python 3 .6?

How can I do this format with a Python 3.6 F-String? person = {'name': 'Jenne', 'age': 23} print('My name {0[name]} and my age {1[age]}'.format(person, person)) print('My name {0} and my age {1}'.format(person['name'], person['age']))
Abou Menah
  • 947
  • 1
  • 6
  • 9
1
2 3
41 42