Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99155

String format - resolve placeholders names

From Ervin Hegedüs <airween@gmail.com>
Newsgroups comp.lang.python
Subject String format - resolve placeholders names
Date 2015-11-20 15:52 +0100
Message-ID <mailman.0.1448031143.2291.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

String format - resolve placeholders names Ervin Hegedüs <airween@gmail.com> - 2015-11-20 15:52 +0100

csiph-web