Daniel Murphy
Daniel Murphy

Reputation: 358

Using '{' as a string in the format method python

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?

Upvotes: 2

Views: 79

Answers (1)

zmbq
zmbq

Reputation: 39039

{{ 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.

Upvotes: 6

Related Questions