Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Format string "}" fill

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}}}}}}'
like image 709
me_and Avatar asked Dec 22 '09 09:12

me_and


1 Answers

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

like image 69
Evan Fosmark Avatar answered Oct 26 '22 09:10

Evan Fosmark