Stupid.Fat.Cat
Stupid.Fat.Cat

Reputation: 11325

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?

Upvotes: 1

Views: 58

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599956

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,)

Upvotes: 10

Dušan Maďar
Dušan Maďar

Reputation: 9909

how about

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

Upvotes: 4

Related Questions