Variable assignment in an f-string

48 Views Asked by At

Question question regarding f-strings and expressions in Python 3.

The Python documentation states clearly that:

Formatted string literals (also called f-strings for short) let you include the value of Python expressions inside a string by prefixing the string with f or F and writing expressions as {expression}.

A valid expression is: {A = "A"} but this doesn't work:

>>> print(f'{A = "A"} {A}')
    File "main.py", line 3
    print(f'{A = "A"} {A}')
          ^
SyntaxError: f-string: expecting '}'

What am I doing wrong here? My expression is valid. I'm sure I'm misunderstanding the documentation so if someone can clarify it would be a great help.

Thanks.

1

There are 1 best solutions below

0
Paul On

You probably want to do this: print(f'A = {A}')