Ok...so, it's kind of easy.
The old method:
num = 6
mystr = 'number is %s' % num
print(mystr) # number is 6
The newer .format method:
num = 6
mystr = "number is {}".format(num)
print(mystr) # number is 6
The .format method using named variable (useful for when sequence can't be depended upon):
num = 6
mystr = "number is {num}".format(num=num)
print(mystr) # number is 6
The shorter f-string method: (thank you @mayur)
num = 6
mystr = f"number is {num}"
print(mystr) # number is 6
Answer from skeetastax on Stack OverflowIs there a way to interpolate variables into a python string WITHOUT using the print function? - Stack Overflow
Does Python do variable interpolation similar to "string #{var}" in Ruby? - Stack Overflow
string - Variable interpolation in Python - Stack Overflow
F-String In Python - A Modern Way To Perform String Interpolation
Videos
Ok...so, it's kind of easy.
The old method:
num = 6
mystr = 'number is %s' % num
print(mystr) # number is 6
The newer .format method:
num = 6
mystr = "number is {}".format(num)
print(mystr) # number is 6
The .format method using named variable (useful for when sequence can't be depended upon):
num = 6
mystr = "number is {num}".format(num=num)
print(mystr) # number is 6
The shorter f-string method: (thank you @mayur)
num = 6
mystr = f"number is {num}"
print(mystr) # number is 6
In each of your cases you have a str object. Its .format method returns the formatted string as a new object. Its __mod__ method, which python calls when it sees the % operator, also returns a formatted string.
Functions and methods return anonymous objects. The context where they are called decide what happens next.
"number is {num}".format(num=num)
throws the result away.
some_variable = "number is {num}".format(num=num)
assigns the result.
some_function("number is {num}".format(num=num))
calls the function with the result as a parameter. print is not a special case - python doesn't do anything special with print.
Interestingly, f-strings like f"number is {num}" is compiled into a series of instructions that build the string dynamically.
Python 3.6+ does have variable interpolation - prepend an f to your string:
f"foo is {bar}"
For versions of Python below this (Python 2 - 3.5) you can use str.format to pass in variables:
# Rather than this:
print("foo is #{bar}")
# You would do this:
print("foo is {}".format(bar))
# Or this:
print("foo is {bar}".format(bar=bar))
# Or this:
print("foo is %s" % (bar, ))
# Or even this:
print("foo is %(bar)s" % {"bar": bar})
Python 3.6 will have has literal string interpolation using f-strings:
print(f"foo is {bar}.")
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' }
The way you're doing it now is a pythonic way to do it. You can also use the locals dictionary. Like so:
>>> fruit = 'Pear'
>>> print("Hey, {fruit}".format(**locals()))
Hey, Pear
Now that doesn't look very pythonic, but it's the only way to achieve the same affect you have in your PHP formatting. I'd just stick to the way you're doing it.
Python 3.6 will add literal string interpolation similar to Ruby's string interpolation. Starting with that version of Python (which is scheduled to be released by the end of 2016), you will be able to include expressions in "f-strings", e.g.
name = "Spongebob Squarepants"
print(f"Who lives in a Pineapple under the sea? {name}.")
Prior to 3.6, the closest you can get to this is
name = "Spongebob Squarepants"
print("Who lives in a Pineapple under the sea? %(name)s." % locals())
The % operator can be used for string interpolation in Python. The first operand is the string to be interpolated, the second can have different types including a "mapping", mapping field names to the values to be interpolated. Here I used the dictionary of local variables locals() to map the field name name to its value as a local variable.
The same code using the .format() method of recent Python versions would look like this:
name = "Spongebob Squarepants"
print("Who lives in a Pineapple under the sea? {name!s}.".format(**locals()))
There is also the string.Template class:
tmpl = string.Template("Who lives in a Pineapple under the sea? $name.")
print(tmpl.substitute(name="Spongebob Squarepants"))
Since Python 2.6.X you might want to use:
"my {0} string: {1}".format("cool", "Hello there!")