Shiv Deepak
Shiv Deepak

Reputation: 3116

what do %(xyz)s representation mean in python

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..

Upvotes: 2

Views: 169

Answers (2)

unwind
unwind

Reputation: 400019

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

Upvotes: 2

Fred Nurk
Fred Nurk

Reputation: 14212

This is string formatting using keys:

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

Upvotes: 6

Related Questions