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
76
votes
5 answers

f-string syntax for unpacking a list with brace suppression

I have been examining some of my string format options using the new f-string format. I routinely need to unpack lists and other iterables of unknown length. Currently I use the following... >>> a = [1, 'a', 3, 'b'] >>> ("unpack a list: " + " {}…
NaN
  • 2,212
  • 2
  • 18
  • 23
72
votes
4 answers

String with 'f' prefix in python-3.6

I'm trying out Python 3.6. Going through new code, I stumbled upon this new syntax: f"My formatting string!" It seems we can do things like this: >>> name = "George" >>> print(f"My cool string is called {name}.") My cool string is called…
DevShark
  • 8,558
  • 9
  • 32
  • 56
70
votes
2 answers

f-string representation different than str()

I had always thought that f-strings invoked the __str__ method. That is, f'{x}' was always the same as str(x). However, with this class class Thing(enum.IntEnum): A = 0 f'{Thing.A}' is '0' while str(Thing.A) is 'Thing.A'. This example…
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
68
votes
5 answers

How can I use f-string with a variable, not with a string literal?

I want to use f-string with my string variable, not with string defined with a string literal, "...". Here is my code: name=["deep","mahesh","nirbhay"] user_input = r"certi_{element}" # this string I ask from user for element in name: …
Deep Ghodasara
  • 768
  • 1
  • 5
  • 9
68
votes
2 answers

Using f-string with format depending on a condition

How can I use f-string with logic to format an int as a float? I would like if ppl is True to format num to 2 decimal places, and if ppl is False to rformat it as whatever it is. Something like string = f'i am {num:.2f if ppl else num}' but this…
jacobcan118
  • 7,797
  • 12
  • 50
  • 95
62
votes
5 answers

What does = (equal) do in f-strings inside the expression curly brackets?

The usage of {} in Python f-strings is well known to execute pieces of code and give the result in string format (some tutorials here). However, what does the '=' at the end of the expression mean? log_file = open("log_aug_19.txt", "w")…
ibarrond
  • 6,617
  • 4
  • 26
  • 45
60
votes
8 answers

Can I import Python's 3.6's formatted string literals (f-strings) into older 3.x, 2.x Python?

The new Python 3.6 f-strings seem like a huge jump in string usability to me, and I would love to jump in and adopt them whole heartedly on new projects which might be running on older interpreters. 2.7, 3.3-3.5 support would be great but at the…
zachd1_618
  • 4,210
  • 6
  • 34
  • 47
56
votes
3 answers

How do I convert a string into an f-string?

I was reading this blog post on Python's new f-strings and they seem really neat. However, I want to be able to load an f-string from a string or file. I can't seem to find any string method or other function that does this. From the example in the…
piRSquared
  • 285,575
  • 57
  • 475
  • 624
49
votes
6 answers

f-strings giving SyntaxError?

I tried this code, following along with a tutorial: my_name = 'Zed A. Shaw' print(f"Let's talk about {my_name}.") But I get an error message highlighting the last line, like so: print(f"Let's talk about {my_name}.") …
Alexander Coffman
  • 503
  • 1
  • 5
  • 8
49
votes
2 answers

How to use gettext with python >3.6 f-strings

Previously you would use gettext as following: _('Hey {},').format(username) but what about new Python's f-string? f'Hey {username}'
mdargacz
  • 1,267
  • 18
  • 32
49
votes
1 answer

how do I use f-string with regex in Python

This code works if I use raw strings only. However, as soon as I add f to r it stops working. Is there a way to make f-strings work with raw strings for re? import re lines = ''' 04/20/2009; 04/20/09; 4/20/09; 4/3/09 Mar-20-2009; Mar 20,…
Jeremy Chen
  • 1,299
  • 2
  • 11
  • 18
44
votes
2 answers

Why were literal formatted strings (f-strings) so slow in Python 3.6 alpha? (now fixed in 3.6 stable)

I've downloaded a Python 3.6 alpha build from the Python Github repository, and one of my favourite new features is literal string formatting. It can be used like so: >>> x = 2 >>> f"x is {x}" "x is 2" This appears to do the same thing as using the…
Aaron Christiansen
  • 11,584
  • 5
  • 52
  • 78
38
votes
5 answers

Transform string to f-string

How do I transform a classic string to an f-string? variable = 42 user_input = "The answer is {variable}" print(user_input) Output: The answer is {variable} f_user_input = # Here the operation to go from a string to an…
François M.
  • 4,027
  • 11
  • 30
  • 81
36
votes
2 answers

Is there a tool to automatically convert string formatting types to f-strings?

Since python 3.6, the shortest and most performant way to format values into a string are PEP 498 f-strings (e.g. f'Hello {name}'). Converting older formatting types ('Hello %s' % name, 'Hello {}'.format(name) ) to the new one in an existing code…
mari.mts
  • 683
  • 5
  • 9
35
votes
2 answers

Why does `'{x[1:3]}'.format(x="asd")` cause a TypeError?

Consider this: >>> '{x[1]}'.format(x="asd") 's' >>> '{x[1:3]}'.format(x="asd") Traceback (most recent call last): File "", line 1, in TypeError: string indices must be integers What could be the cause for this behavior?
d33tah
  • 10,999
  • 13
  • 68
  • 158
1
2
3
41 42