2

I want to be able to print something like such "{x}" using the format method, but the nature of the curly braces is messing me up.

I tried

'{{}}'.format(x)

however that returned a value error. Is there a way to tell python that the curly brace is meant to be used as a string rather than an argument for the format?

0

1 Answer 1

6

{{ is converted into {by format, so use this:

'{{{}}}'.format(x)

(note the three braces)

However, in this case, I would use the older C-style format string:

'{%s}' % x

It is a lot clearer.

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

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.