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
4
votes
3 answers

How to specify string format with reference to the variable type at the time of formatting?

If there is a variable a whose type is unknown until it is created, how to account for the type when using python string formatting, specifically at the time of formatting? Example: import numpy as np import datetime as dt a =…
agftrading
  • 784
  • 3
  • 8
  • 21
4
votes
1 answer

Space in f-string leads to ValueError: Invalid format specifier

A colleague and I just stumbled across an interesting problem using an f-string. Here is a minimal example: >>> f"{ 42:x}" '2a' Writing a space after the hexadecimal type leads to a ValueError: >>> f"{ 42:x }" Traceback (most recent call last): …
Stefan Scheller
  • 953
  • 1
  • 12
  • 22
4
votes
1 answer

Why can't you put quotes inside a python f-string?

Why is f'{'one'}' invalid syntax? Seems to me like if f-strings were sensible, as soon as a { appears, everything inside of { ... } should be considered Python code. What's the reason the language designers decided to make the string's end quote be…
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
4
votes
1 answer

Python f-string with a Variable Decimal Number Specifier

Is it possible to use Python f-string with a variable decimal number specifier? Here is an example, but it does not work: print(f'{1.23456:.2f}') # working f-string code number = 2 print(f'{1.23456:.' + number + 'f}') Python3.7.
demirod
  • 81
  • 15
4
votes
2 answers

is f"{expr=}" a new f-string expression (see equal sign before training brace)? from which version of python on?

I have received a python notebook that is full of expressions like f"{expr=}" that in my environment produce error messages: var=7 print(f"{var=}") File "", line 1 (var=) ^ SyntaxError: invalid syntax I suspect/expect that…
lurix66
  • 502
  • 1
  • 5
  • 14
4
votes
1 answer

f-string debugging shorthand in Python 3.6

The following works in Python 3.8+ a = 1.5 print(f'{a=}') In earlier Python versions it is equivalent to a = 1.5 print(f'a={a}') I developed my library for Python 3.8+ but a few servers (CentOS-7, OpenSUSE-15.1/15.2) have Python 3.6 by default,…
Dilawar
  • 5,438
  • 9
  • 45
  • 58
4
votes
2 answers

Can I assert the python version before the interpreter parses the code?

I want to inform the user which python version they should use: import sys assert sys.version_info >= (3, 6), "Use Python 3.6 or newer" print(f"a format string") But instead, running the above file makes a syntax error: $ python fstring.py. #…
Alex Shroyer
  • 3,499
  • 2
  • 28
  • 54
4
votes
2 answers

Python3 f-strings: how to avoid having to escape literal curly brackets?

Is there a way to avoid having to escape literal curly brackets characters in a python3 f-string? For example if you want to output a json string or a chunk of CSS rules it's really inconvenient to have to convert all the { and } characters to {{…
ccpizza
  • 28,968
  • 18
  • 162
  • 169
4
votes
1 answer

Combining f-string with raw string to be used inside regex gives SyntaxError; ValueError or wrong result

I have a string here : s0 = 'Ready1 Origin1 Destination1 Type1 Rate1 Phone1 # Pro1 #' and the following variable being calculated like this : is_head = len([i.group() for i in re.finditer(r"(\s+){2,}",…
Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
4
votes
4 answers

Is there a way to include a comment in an f-string?

It would be useful for mo to include a comment in an f-string. For instance, take this code: f""" bla """ It would be nice if this code was equivalent…
Flimm
  • 136,138
  • 45
  • 251
  • 267
4
votes
1 answer

Why do print and f-string perform evaluation at different times?

Does the evaluation of variables in print and f-string happen at different times? I expected it to be the same output for both. def foo(x): x.append([5]) return x y, z = [1], [3] print('y:', y, 'z:', z) # y: [1] z: [3] print('y:', y,…
Teebu
  • 677
  • 7
  • 18
4
votes
2 answers

F string autocomplete python

When using f strings in Python 3 for Atom, it doesn't autocomplete the string correctly. Typing in types_of_people = 10 x = f"There are {types_of_people} types_of_people." I get x = f"... when I start typing but the end quote doesn't…
Jinzu
  • 1,325
  • 2
  • 10
  • 22
4
votes
1 answer

Escaping chars in f-string

I have run into the following issue with the f-string: >>> a='hello' # how to print '{hello}' ? >>> f'{{a}}' '{a}' >>> f'\{{a}\}' File "", line 1 SyntaxError: f-string: single '}' is not allowed # doing it without f-strings >>> '{' + a…
user12282936
  • 167
  • 2
  • 7
4
votes
3 answers

Why does VSCode Code Runner not support f-strings?

I have this issue with VSCode and the Code Runner extension and here is the code snippet causing the issue: class init_error(Exception): def __init__(self, comp="Internals"): self.component = comp self.message = f"Error occurred…
4
votes
3 answers

F string list comprehensions example needed

I have a list of floats that I would like to print using an f-string: Here is the list er=[0.3401126304419511, 0.32561695086083536, 0.3115512789515306, 0.21734184387097139, 0.22354269408531188] and I want to print them using an f-string as…
P Moran
  • 1,624
  • 3
  • 18
  • 32