String format - resolve placeholders names

Ervin Hegedüs airween at gmail.com
Fri Nov 20 09:52:27 EST 2015


Hi,

Python has a good string formatter, eg. I can do this:

s = "{who} likes {what}"
d = {'who': "Adam", 'what': "ants"}
s.format(**d)

result:
'Adam likes ants'

Is it possible, and if yes, how to resolve the placeholders names
in string?

There is a know method:

d1 = {'who1': "Adam", 'what1': "ants"}
try:
    s.format(**d1)
except KeyError:
    print("keyword missing")

(gives 'keyword missing' as result).


But is there any other (direct) way, which keywords exists in
string?


Thanks,


a.





More information about the Python-list mailing list