Is it possible to capitalize a word using string formatting? For example,
"{user} did such and such.".format(user="foobar")
should return "Foobar did such and such."
Note that I'm well aware of .capitalize()
; however, here's a (very simplified version of) code I'm using:
printme = random.choice(["On {date}, {user} did la-dee-dah. ",
"{user} did la-dee-dah on {date}. "
])
output = printme.format(user=x,date=y)
As you can see, just defining user
as x.capitalize()
in the .format()
doesn't work, since then it would also be applied (incorrectly) to the first scenario. And since I can't predict fate, there's no way of knowing which random.choice
would be selected in advance. What can I do?
Addt'l note: Just doing output = random.choice(['xyz'.format(),'lmn'.format()])
(in other words, formatting each string individually, and then using .capitalize()
for the ones that need it) isn't a viable option, since printme
is actually choosing from ~40+ strings.
format_map
and supply adict
-like object that does a lot of various kinds of work to bring back a value with many other values in the format template. I can't write to thedict
-like not simply like that. I don't want to have to parse the string template just to find out what values I need to pull out of thedict
-like in advance just so I can case one of the values first - that would defeat the whole point. – Tien