Python: Capitalize a word using string.format()

前端 未结 4 2146
萌比男神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:04

    You can pass extra values and just not use them, like this lightweight option

    printme = random.choice(["On {date}, {user} did la-dee-dah. ",
                             "{User} did la-dee-dah on {date}. "
                             ])
    
    output = printme.format(user=x, date=y, User=x.capitalize())
    

    The best choice probably depends whether you are doing this enough to need your own fullblown Formatter.

提交回复
热议问题