Variable interpolation in Python [duplicate]

The closest you can get to the PHP behaviour is and still maintaining your Python-zen is:

print "Hey", fruit, "!"

print will insert spaces at every comma.

The more common Python idiom is:

print "Hey %s!" % fruit

If you have tons of arguments and want to name them, you can use a dict:

print "Hey %(crowd)s! Would you like some %(fruit)s?" % { 'crowd': 'World', 'fruit': 'Pear' }

Leave a Comment