If I try to do the following:
things = 5
print("You have " + things + " things.")
I get the following error in Python 3.x:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be str, not int
... and a similar error in Python 2.x:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
How can I get around this problem?
TL;DR
either:
print("You have " + str(things) + " things.")
(the old school way)or:
print("You have {} things.".format(things))
(the new pythonic and recommended way)A bit more verbal explanation:
Although there is anything not covered from the excellent @Zero Piraeus answer above, I will try to "minify" it a bit:
You cannot concatenate a string and a number (of any kind) in python because those objects have different definitions of the plus(+) operator which are not compatible with each other (In the str case + is used for concatenation, in the number case it is used to add two numbers together). So in order to solve this "misunderstanding" between objects:
str(anything)
method and then concatenate the result with another string.Have fun and do read the @Zero Piraeus answer it surely worth your time!
Python 2.x
'You have %d things.' % things
[1]'You have {} things.'.format(things)
[2]Python 3.6+
'You have %d things.' % things
[1]'You have {} things.'.format(things)
[2]f'You have {things} things.'
[3]Reference
The problem here is that the
+
operator has (at least) two different meanings in Python: for numeric types, it means "add the numbers together":... and for sequence types, it means "concatenate the sequences":
As a rule, Python doesn't implicitly convert objects from one type to another1 in order to make operations "make sense", because that would be confusing: for instance, you might think that
'3' + 5
should mean'35'
, but someone else might think it should mean8
or even'8'
.Similarly, Python won't let you concatenate two different types of sequence:
Because of this, you need to do the conversion explicitly, whether what you want is concatenation or addition:
However, there is a better way. Depending on which version of Python you use, there are three different kinds of string formatting available2, which not only allow you to avoid multiple
+
operations:... but also allow you to control how values are displayed:
Whether you use % interpolation,
str.format()
, or f-strings is up to you: % interpolation has been around the longest (and is familiar to people with a background in C),str.format()
is often more powerful, and f-strings are more powerful still (but available only in Python 3.6 and later).Another alternative is to use the fact that if you give
print
multiple positional arguments, it will join their string representations together using thesep
keyword argument (which defaults to' '
):... but that's usually not as flexible as using Python's built-in string formatting abilities.
1 Although it makes an exception for numeric types, where most people would agree on the 'right' thing to do:
2 Actually four ... but template strings are rarely used and somewhat awkward.