How to Escape Curly-Brackets in F-Strings

How do I print curly-brace characters in a string while using .format?

You need to double the {{ and }}:

>>> x = " {{ Hello }} {0} "
>>> print(x.format(42))
' { Hello } 42 '

Here's the relevant part of the Python documentation for format string syntax:

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 to escape curly-brackets in f-strings?

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.

Python f-strings: how to escape curly brackets with numbers in it

Unless there are more constraints you haven't mentioned, this just doesn't have to be an f-string at all:

_date = '''
SELECT
{acol},
substring(full_date , '[0-9]{{4}}')
FROM
dates
'''

formatted = _date.format(acol="some_col")

print(formatted)

prints


SELECT
some_col,
substring(full_date , '[0-9]{4}')
FROM
dates

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

One can use implicit string concatenation to apply f-string formatting to only parts of a string.

[...] Also note that literal concatenation can use different quoting styles for each component (even mixing raw strings and triple quoted strings), and formatted string literals may be concatenated with plain string literals.

>>> hello = "world"
>>> "{hello}" f"{hello}" "!"
'{hello}world!'

This uses the exact same bytecode as the respective f-string with escapes.
Note that the whitespace is optional, but helpful for readability.

f-string with curly brace and function

You need a triple curly. Each double curly makes for a single escaped curly braces:

>>> f"{{{1}}}"
'{1}'

Escaping brackets in strings python fstrings

Try and break the statement into simpler components. It looks cleaner which would be the point of using f-strings.

Otherwise, if you need to escape "{" in f-string, you can use "{{".
Eg.

s = "{{{0}" # is "{0"
s = "{0}}}" # is "0}"

Why are double curly braces used instead of backslash in python f-strings?

I believe that PEP 536 (Final Grammar for Literal String Interpolation) speaks to this point:
https://www.python.org/dev/peps/pep-0536/

A short snippet from the Motivation section of the PEP is "The current implementation of f-strings in CPython relies on the existing string parsing machinery and a post processing of its tokens. This results in several restrictions to the possible expressions usable within f-strings: "

For additional information refer to this linked email.

Python string formatting issue with curly braces ({ and })

Use double-curly-brace instead of single-curly-brace to write a literal curly-brace in an f-string:

dev_id = 1
query = f"""
{{
scripts(developers: "{dev_id}") {{

...
...
}}
}}
"""
print(query)
# {
# scripts(developers: "1") {
#
# ...
# ...
# }
# }



Related Topics



Leave a reply



Submit