Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape curly-brackets in f-strings? [duplicate]

I have a string in which I would like curly-brackets, but also take advantage of the f-strings feature. Is there some syntax that works for this?

Here are two ways it 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' is not defined

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

SyntaxError: f-string expression part cannot include a backslash

Desired result:

'test {bar}'

Edit: Looks like this question has the same answer as How can I print literal curly-brace characters in a string and also use .format on it?, but you can only know that if you know that str.format uses the same rules as the f-string. So hopefully this question has value in tying f-string searchers to this answer.

like image 909
JDAnders Avatar asked Mar 01 '17 00:03

JDAnders


People also ask

How do you escape curly brackets in F strings?

Format strings contain “replacement fields” surrounded by curly braces {} . Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }} .

How do I get rid of curly brackets in Python?

To remove square brackets from the beginning and end of a string using Python, we pass “[]” to the strip() function as shown below. If you have curly brackets as well, we pass “[]{}” to strip() to remove the brackets.

How do you use curly braces in Python string?

The curly braces show where the inserted value should go. You can insert more than one value. The values do not have to be strings, they can be numbers and other Python objects. >>>

Can we use curly braces in Python?

Easily my favorite advanced feature in Python, rather than relying on whitespace to denote scopes (boring) — we can use curly braces!

How to escape curly braces in F-strings in Python?

Now, what if we need to escape curly braces in f-strings in Python. For this, there is a simple fix of using two curly braces instead of one. This way, we can print the curly braces in the output. However, this way we cannot provide the missing values.

What version of python do you need to escape Curly brackets?

If you use Python version 3.9.0a4 or greater, then you can directly use: How to escape curly-brackets in f-strings? StackOverflow: Questions

What is the use of F-strings in Python?

String formatting is also used for replacing and filling in missing values. Python 3.6 introduced a new method to format strings called f-strings. The f-strings provide a quick and efficient way to format strings in Python. It is considered faster than the other methods of format () function and the % operator.


1 Answers

Although there is a custom syntax error from the parser, the same trick works as for calling .format on regular strings.

Use double curlies:

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

It's mentioned in the spec here and the docs here.

like image 144
wim Avatar answered Oct 10 '22 05:10

wim