views:

324

answers:

1

Python 2.6 defines str.format(…), which, from Python 3.0, is preferred to the old % style of string formatting. Looking at the "mini-language", however, it seems that it is impossible to use the } character as a fill character.

This seems like a strange omission to me. Is it possible to somehow escape that character and use it as a fill character?

I know I could fill with some unique-to-the-string character and then use str.replace, or any number of other similar tricks to achieve the same result, but that feels like a hack to me…

Edit: What I'm after is something like

>>> "The word is {0:}<10}".format("spam")
'The word is spam}}}}}}'
+5  A: 

You could always specify it as a separate parameter like so:

>>> "The word is {0:{1}<10}".format("spam", "}")
'The word is spam}}}}}}'

See, it gets passed in and used in place of {1}.

Evan Fosmark