Python: Capitalize a word using string.format()

前端 未结 4 2071
萌比男神i
萌比男神i 2021-02-01 06:39

Is it possible to capitalize a word using string formatting? For example,

\"{user} did such and such.\".format(user=\"foobar\")

should return \

4条回答
  •  遥遥无期
    2021-02-01 07:10

    You can create your own subclass of string.Formatter which will allow you to recognize a custom conversion that you can use to recase your strings.

    myformatter.format('{user!u} did la-dee-dah on {date}, and {pronoun!l} liked it. ',
                          user=x, date=y, pronoun=z)
    

提交回复
热议问题