-1

I want to format a string with a few variables. But I have more text in the string that is surround by curly braces, but I don't want to format that. I have tried writing it with backslashes: \{content\} but that didn't work either. How do I achieve this?

Example:

string = \
"""
{user}'s email is {email}.
{This} is in {curly} {braces}.
"""

print(string.format(user="Foo", email="Bar"))

I don't want to format "This", "curly", and "braces".

ArjunSahlot
  • 426
  • 5
  • 13

2 Answers2

3

If you want keep curly braces you should try with

string = \
"""
{user}'s email is {email}.
{{This}} is in {{curly}} {{braces}}.
"""

print(string.format(user="Foo", email="Bar"))
# Foo's email is Bar.
# {This} is in {curly} {braces}.
jia Jimmy
  • 1,693
  • 2
  • 18
  • 38
2

The brace can be escaped by doubling: {{ and }}.

string = \
"""
{user}'s email is {email}.
{{This}} is in {{curly}} {{braces}}.
"""

print(string.format(user="Foo", email="Bar"))

Here is the relevant doc: python format string syntax

lee
  • 61
  • 5