Why is it using Python F-string to interpolate 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 do two cases print these quotes?

I did not find anything explicitly in the documentation. The closest I found is described in detail in PEP for this function:

(grammar for F-lines)

f ' <text> { <expression> <optional !s, !r, or !a> <optional : format specifier> } <text> ... '

The expression is then formatted using the format protocol , using the format specifier as an argument. The resulting value is used when building the value of the f-string.

I believe the value is aformatted using some formatting, which, since the data type is a string, wraps it in quotation marks. This result then returns to the surrounding F-string formatting instance.

? - , ?

+4
1

f'{ {a} }' {a} ( ) Python. Python {a} a set (a), str repr , .

+7

Source: https://habr.com/ru/post/1681492/


All Articles