Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

escape single bracket in f-string

I am trying to print a string in the following format:

"rob: {color: red  , number: 4}"

using an f-string to fill three values:

f"{name}: color: {color}  , number: {number}" #missing brackets

this would print:

"rob: color: red  , number: 4" #missing brackets

but I have no idea how to escape a single bracket the way I need to. I know {{}} lets you escape brackets, but this would print both at the same place in the string. I tried { { } and { } } in their respective spots but this just threw an error.

like image 352
Justin Avatar asked Oct 13 '25 09:10

Justin


1 Answers

Goal:

"rob: {color: red  , number: 4}"

Accomplished:

>>> name = 'rob'
>>> color = 'red'
>>> number = 4
>>> print(f"{name}: {{color: {color} , number: {number}}}")
rob: {color: red , number: 4}
like image 94
Maximilian Burszley Avatar answered Oct 14 '25 21:10

Maximilian Burszley