5

How can I include { symbol in a string?

My string

str2="call_function(Macro, {0});\n \{ \t int n;\n".format("value")

I'm getting the error:

ValueError: unmatched '{' in format

I have escaped the { symbol and have tried without escaping too. I'm getting the same error in both cases.

2 Answers 2

16

Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}. docs


In [1]: '{{{0}'.format('foo')
Out[1]: '{foo'
Sign up to request clarification or add additional context in comments.

3 Comments

Hey thanks for the useful tip. Works perfect. Any other escaping tricks that I need to know ?
@Abhishek -- Anything that is not contained in braces is considered literal text, which is copied unchanged to the output.. Anyway, I think it would be useful to chew yourself through the docs...
0

also can do like this

'{%s' % 'foo'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.