开发者

what do %(xyz)s representation mean in python

开发者 https://www.devze.com 2023-01-25 12:29 出处:网络
What are these representation in python and how to use. My guess is it is used for data validation but never able to use it.

What are these representation in python and how to use. My guess is it is used for data validation but never able to use it.

%(name)s
%(levelno)s
%(levelname)s
%(pathname)s开发者_运维百科
%(process)d 
etc..


This is string formatting using keys:

>>> d = {"answer": 42}
>>> "the answer is %(answer)d" % d
'the answer is 42'


It's formatting a string, by picking values from a dictionary:

test = { "foo": 48,  "bar": 4711, "hello": "yes" }
print "%(foo)d %(bar)d and %(hello)s" % test

prints:

48 4711 and yes
0

精彩评论

暂无评论...
验证码 换一张
取 消