3

I need some help with the Python's new string formatter. Basically, I want the outer curly braces to not escape the string replacement field.

This works:

foo = 'bar'
print '{%s}' % foo

But, this doesn't:

foo = 'bar'
print('{{0}}'.format(foo))

Desired output:

'{bar}'

0

2 Answers 2

7

It looks like you want:

>>> foo = 'bar'
>>> print('{{{0}}}'.format(foo))
'{bar}'

The outer pair of doubled {{ and }} are copied literally to the output, leaving {0} to be interpreted as a substitution.

Sign up to request clarification or add additional context in comments.

1 Comment

Ha, sometimes I tend to over complicate things. Thanks.
4

{{ is an escaped {, but you still need {0} as the placeholder, thus:

print('{{{0}}}'.format('bar'))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.