'Why is print("text" + str(var1) + "more text" + str(var2)) described as "disapproved"?

Why is the code below termed 'age-old disapproved method' of printing in the comment by 'Snakes and Coffee' to Blender's post of Print multiple arguments in python? Does it have to do with the backend code/implementation of Python 2 or Python 3?

print("Total score for " + str(name) + " is " + str(score))


Solution 1:[1]

Adding many strings is disapproved because:

  • it's not really readable, compared to the alternatives.
  • it's not as efficient as the alternatives.
  • if you have other types you have to manually call str on them.

And, yeah, it is really old. :-)

In theory string addition creates a new string. So, just assume you add n strings, then you need to create n-1 strings but all of these except one are discarded because you're only interested in the final result. Strings are implemented as arrays so you have a lot of potentially expensive (re-)allocation for no benefit.

If you have a string with placeholders it is not only more readable (you don't have these + and str between them) but python can also compute how long the final string is and allocate only one array for the final string and insert everything.

In practice that's not really what is happening because Python checks if a string is an intermediate and does some optimization. So it's not as bad as creating n-2 unnecessary arrays.

For small strings and/or interactive use you wouldn't even notice a difference. But then the other ways have the advantage of being more readable.

Alternatives could be (the first two are copied from @MKemps answer):

  • "Total score for {} is {}".format(name, score)
  • "Total score for %s is %s" % (name, score) (also old!)
  • "Total score for {name} is {score}".format(name=name, score=score)
  • f"Total score for {name} is {score}" (very new - introduced in Python 3.6)

Especially the latter two examples show that you can even read the template string without having to insert anything.

Solution 2:[2]

It is considered old because you can use 'better' ways to format it with the introduction of python 3 (and later versions of python 2).

print("Total score for "+str(name)"+ is "+str(score))

Could be written as: print("Total score for %s is %s" % (name, score))

Although there are a multitude of different ways you can format print in later versions of python 2 and above.

What is up above is technically old as well, this is another way to do it in later versions of python 2 and above.

print('Total score for {} is {}'.format(name, score)

Solution 3:[3]

The print function accepts any number of arguments, which will be converted to strings automatically and (by default) be printed with spaces in between them. So it is simpler to write the example code this way:

print("Total score for", name, "is", score)

Note that the leading and trailing spaces are not included in the string literals, because print will add those spaces automatically. If you don't want those automatic extra spaces (or you want a different separator), then you can pass the optional keyword argument sep at the end:

print("Score for ", name, " is ", score, "/100", sep="")
# Score for (name) is (score)/100

Note that this time the string literals do need to include the spaces where we want them, since we told the print function not to add them automatically.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Tournoij
Solution 2
Solution 3 kaya3