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
7
votes
2 answers

SyntaxError: f-string: expecting '}'

I have a problem here. I don't know why this code does not work. newline = '\n' tasks_choosen = ['markup', 'media', 'python_api', 'script', 'style', 'vue'] print(f'{ newline }### Initializing project with the following tasks: { '…
RamoFX
  • 510
  • 2
  • 9
  • 17
7
votes
3 answers

Truncate f-string float without rounding

I want to print a very very close-to-one float, truncating it to 2 decimal places without rounding, preferably with the least amount of code possible. a = 0.99999999999 print(f'{a:0.2f}') Expected: 0.99 Actual: 1.00
Michael Bianconi
  • 5,072
  • 1
  • 10
  • 25
7
votes
1 answer

Does PHP have a string function like Python's f-string function? (not str.format())

I'm new to PHP, so please excuse the question. I was wondering if PHP had a string format function such as Python's f-strings function, not str.format(). I have seen a few posts regarding the subject, but most of the examples accepted as answers…
artomason
  • 3,625
  • 5
  • 20
  • 43
7
votes
1 answer

How are f-strings handled in implicit string concatenation?

I made this mistake: key, value = 'K', 999 msg = ( f"key={key}, " "value={value}" # needs to be prefixed with f as well ) # key=K, value={value} and started wondering how Python handles complex cases of literal concatenation. Let's…
VPfB
  • 14,927
  • 6
  • 41
  • 75
7
votes
2 answers

Choose the number of decimal points in string interpolation

Is there a way to use a variable to decide the number of decimal points in literal string interpolation? for example if I have something like f'{some_float:.3f}' is there a way to replace the 3 with a variable? The end goal is to add data labels to…
Dan
  • 45,079
  • 17
  • 88
  • 157
7
votes
1 answer

Is there any way to break up an f-string within the braces?

For example, if I have >>> name = f"{os.path.splitext(os.path.basename('/some/long/path/I/donot/need/to/some/config.bs'))[0]}.yaml" 'config.yaml' Because there's very little actual text, there's no good place prior to 79 characters to break the…
Wyrmwood
  • 3,340
  • 29
  • 33
6
votes
1 answer

Displaying floats using F-string

I'm really curious about why the behaviour is so different when add this "f" to the end of the number which I want to display: # CASE with f float1 = 10.1444786 print(f"{float1:.4f}") # print out: 10.1445 # CASE without f print(f"{float1:.4}") #…
Vesa95
  • 607
  • 1
  • 7
  • 26
6
votes
3 answers

How to skip trailing zeroes from a decimal with f-strings

I want to set a precision in my f-strings, but I do not want the the trailing zeroes in the decimal part. i = 1002 print(f'{i/1000:.2f}') # 1.00 but this must be 1 i = 1009 print(f'{i/1000:.2f}') # 1.01, this is correct The first print must be 1,…
Lorena
  • 75
  • 1
  • 7
6
votes
3 answers

How to use f-string formatting around a string of a dictionary

The following code causes an Invalid Format Specifier when the string is converted to an f-string. I can't pinpoint what the problem is as my quotations look okay. expected_document = f'{"name":"tenders","value":"chicken","key":"{key}"}' causes: > …
tenders27
  • 61
  • 1
  • 2
6
votes
3 answers

Preserving indentation in a triple-quoted fstring

I am trying to use a triple-quoted strings in Python3(.7) to build some formated strings. I have a list of inner strings, which all need to be tabbed in: This is some text across multiple lines. And a string which should contain the…
6
votes
0 answers

Is there a way to add Python f-string support to Visual Studio 2017?

I'm switching back to using VS2017 for most of my Python development after using VSCode for the majority of the time. In VSCode I was able to use f-strings nicely with syntax highlighting and intellisense. In VS2017, when I use an f-string, it'll…
Phillip McMullen
  • 453
  • 2
  • 5
  • 15
6
votes
2 answers

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…
JS.
  • 14,781
  • 13
  • 63
  • 75
6
votes
1 answer

Why is this usage of python F-string interpolation wrapping with quotes?

Code in question: a = 'test' # 1) print(f'{a}') # test # 2) print(f'{ {a} }') # {'test'} # 3) print(f'{{ {a} }}') # {test} My question is, why does case two print those quotes? I didn't find anything explicitly in the documentation. The closest…
5
votes
2 answers

How to convert float to string with specific number of decimal places in Python polars?

I have a polars DataFrame with multiple numeric (float dtype) columns. I want to write some of them to a csv with a certain number of decimal places. The number of decimal places I want is column-specific. polars offers format: import polars as…
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
5
votes
2 answers

Why are f-strings slower than string concatenation when repeatedly adding to a string inside a loop?

I was benchmarking some code for a project with timeit (using a free replit, so 1024MB of memory): code = '{"type":"body","layers":[' for x, row in enumerate(pixels): for y, pixel in enumerate(row): if pixel != (0, 0, 0, 0): …