Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string.format() with {} inside string as string [duplicate]

Tags:

python

string

Ponder that you have a string which looks like the following 'This string is {{}}' and you would like to transform it into the following 'This string is {wonderful}'

if you do 'This string is {{}}'.format('wonderful') it won't work. What's the best way to achieve this?

like image 366
Jonathan Avatar asked Oct 22 '15 16:10

Jonathan


3 Answers

You just need one more pair of {}

'This string is {{{}}}'.format('wonderful')
like image 120
Morgan Thrapp Avatar answered Nov 11 '22 23:11

Morgan Thrapp


you need triple brackets: two for the literal { and }and the pair in the middle for the format function.

print('This string is {{{}}}'.format('wonderful'))
like image 25
hiro protagonist Avatar answered Nov 11 '22 23:11

hiro protagonist


Two brackets to get {} in line (escaping), and third as placeholder:

'This string is {{{}}}'.format('wonderful')
like image 1
Eugene Soldatov Avatar answered Nov 11 '22 21:11

Eugene Soldatov