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
18
votes
1 answer

How to merge f string with b string in one line usage in Python

I can create b-sting this way: name_binary = b'Adam' but if I have variable like name='Adam' and I want to make at once usage of f-string and b-string: name_binary = fb'{name}' I get: File "", line 1 c = fb'{a}' …
pbaranski
  • 22,778
  • 19
  • 100
  • 117
17
votes
4 answers

f-string: unmatched '(' in line with function call

I'm trying to use f-strings in python to substitute some variables into a string that I'm printing, and I'm getting a syntax error. Here's my code: print(f"{index+1}. {value[-1].replace("[Gmail]/", '')}") I only started having the problem after I…
Cameron Delong
  • 454
  • 1
  • 6
  • 12
17
votes
1 answer

How to display LaTeX f-strings in matplotlib

In Python 3.6, there is the new f-string to include variables in strings which is great, but how do you correctly apply these strings to get super or subscripts printed for matplotlib? (to actually see the result with the subscript, you need to draw…
will.mendil
  • 752
  • 2
  • 6
  • 21
17
votes
5 answers

How to interpolate a list into an f-string in Python?

Say I have a function def foo(): return [1, 2, 3] I want to interpolate the result of the function into a string to get "001 002 003". I've tried this: f"{*foo():03d 03d 03d}" But it produced SyntaxError: can't use starred expression here. Can I…
planetp
  • 14,248
  • 20
  • 86
  • 160
16
votes
2 answers

How to print binary numbers using f"" string instead of .format()?

For printing some numbers to their binary formats, we simply use the .format() method, like so: # Binary for i in range(5+1): print("{0:>2} in binary is {0:>08b}".format(i)) Output: 0 in binary is 00000000 1 in binary is 00000001 2 in binary is…
shiv_90
  • 1,025
  • 3
  • 12
  • 35
16
votes
1 answer

Why isn't it possible to use "await" in f-strings?

Why isn't it possible to use "await" in f-strings? Is there any way to coerce f-strings into evaluating the format expressions in the context of a coroutine function? $ python3 Python 3.6.0 (default, Mar 4 2017, 12:32:37) [GCC 4.2.1 Compatible…
lyschoening
  • 18,170
  • 11
  • 44
  • 54
15
votes
1 answer

Can you overload the Python 3.6 f-string's "operator"?

In Python 3.6, you can use f-strings like: >>> date = datetime.date(1991, 10, 12) >>> f'{date} was on a {date:%A}' '1991-10-12 was on a Saturday' I want to overload the method receiving the '%A' above. Can it be done? For example, if I wanted to…
user3002273
14
votes
1 answer

Format variable in f-string with a variable number of decimal places

I need to format the number of decimal places that a variable displays in an f-string using a variable for the number of places. n = 5 value = 0.345 print(f'{value:.4f}') Instead of value.4f, I need value.nf where n is the number of decimal places…
Mike C.
  • 1,761
  • 2
  • 22
  • 46
13
votes
1 answer

f-string formula inside curly brackets not working

I am on Python 3.7. My code on line 3 works fine, however when I insert the underlying formula into line 4, my code returns the error: SyntaxError: f-string: mismatched '(', '{', or '[' (the error points to the first '(' in line 4. My code…
Deskjokey
  • 568
  • 1
  • 7
  • 18
13
votes
6 answers

Python fstring as function

I'd like to use Python f-string for its syntactical simplicity, compared to string.Template() or other approach. However, in my application, the string is loaded from file, and the values of the variable can only be provided later. If there a way to…
idazuwaika
  • 2,749
  • 7
  • 38
  • 46
13
votes
1 answer

Why does the symbol '{' remain when f"\{10}" is evaluated in Python 3.6?

The f-string is one of the new features in Python 3.6. But when I try this: >>> f"\{10}" '\\{10' I can't figure out why the left curly brace '{' remains in the result. I supposed that the result should be same with the str.format: >>>…
lazzzis
  • 141
  • 6
12
votes
2 answers

Why do f-strings require brackets around assignment expressions?

In Python (3.11) why does the use of an assignment expression (the "walrus operator") require wrapping in brackets when used inside an f-string? For example: #!/usr/bin/env python from pathlib import Path import torch DEVICE =…
Anil
  • 1,097
  • 7
  • 20
12
votes
3 answers

What does 'f' mean before a string in Python?

I'm new here and also new to Python. I wonder what f in print(f'Column names are {"-".join(row)}') does. I tried deleting it and then Column names are {"-".join(row)} become normal string. Could you please tell me what f calls, so I can google to…
KradasA4
  • 165
  • 1
  • 2
  • 11
12
votes
2 answers

python 3.6+ logger to log pandas dataframe - how to indent the entire dataframe?

I need to use python logging module to log pandas dataframe. I need the entire dataframe (all rows) indented equally. Below is the simple desired output: Test Dataframe Output Below: col1 col2 0 1 3 1 2 4 However, I…
user32147
  • 1,033
  • 4
  • 12
  • 22
12
votes
2 answers

How to evaluate a variable as an f-string?

I would like to have a mechanism which evaluates an f-string where the contents to be evaluated are provided inside a variable. For example, x=7 s='{x+x}' fstr_eval(s) For the usage case I have in mind, the string s may arise from user input…
Ben Mares
  • 1,764
  • 1
  • 15
  • 26