开发者

Python Format string "}" fill

开发者 https://www.devze.com 2022-12-15 01:54 出处:网络
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 }

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消