How do I add curly brackets to f-strings in Python 3?

Viewed 4610

I have this code

class = "maximum"
s = f"""The code for {class} is {3854-st56}"""
print(s)

I want this to output:

>> The code for maximum is {3854-st56}

But the f-string doesn't recognize the curly brackets around 3854-st56 and instead thinks I am trying to input actual Python code. How do I make the f-string recognise that the curly brackets are string literals.

2 Answers

You need to use curly bracket twice if you are assigning variable value and once if you are putting on a value:

Class = "maximum"
s = f"""The code for {{{Class}}} is {{'3854-st56'}}"""
print(s)

The code for {maximum} is {3854-st56}
Related