TAGS :Viewed: 11 - Published at: a few seconds ago

[ Python: how do I pass one parameter for the string instead of having multiple repeated values? ]

Instead of having:

"my name is %s, and your name is %s" %(name, name)

is it possible to just have

"my name is %s, and your name is %s" %(name)

that would fill both %s with the name variable?

Answer 1


You can use a dictionary:

"my name is %(name)s, and your name is %(name)s" % {'name': name}

but better to use the new .format method:

"my name is {name}, and your name is {name}".format(name=name)

or

"my name is {0}, and your name is {0}".format(name,)

Answer 2


how about

"my name is {name}, and your name is {name}".format(name=name)