Python formatting string "}" fill

Python 2.6 defines str.format(…) , which from Python 3.0 is preferable to the old style % format string. Looking at mini-language ", it seems that the character } cannot be used as a pad character.

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

I know that I can fill in some unique character and then use str.replace or any number of other similar tricks to achieve the same result, but this seems like a hack for me ...

Edit: for me something like

 >>> "The word is {0:}<10}".format("spam") 'The word is spam}}}}}}' 
+4
source share
1 answer

You can always specify it as a separate parameter:

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

See, it is passed and used instead of {1}.

+7
source

All Articles