How to avoid f-lines in python 3.6?

I have a line where I need curly braces, but also use the f-strings function. Is there any syntax that works for this?

Here are two ways this does not work. I would like to include the literal text " {bar} " as part of the string.

 foo = "test" fstring = f"{foo} {bar}" 

NameError: name 'bar' not defined

 fstring = f"{foo} \{bar\}" 

Syntax Error: part of an f-string expression cannot contain a backslash

Desired Result:

 'test {bar}' 

Edit: this question seems to have the same answer as How can I print letter shapes in curly braces in a python string, and also use .format on it? but you can only know that if you know that the format function uses the same rules as the f-string. So hopefully this question is relevant for linking f-string finders to this answer.

+6
source share
1 answer

Although the parser has a special syntax error, the same trick works as for regular strings.

Use double curls:

 >>> foo = 'test' >>> f'{foo} {{bar}}' 'test {bar}' 

The spec is mentioned here and the docs here .

+13
source

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


All Articles