Computer >> Computer tutorials >  >> Programming >> Python

How can I concatenate str and int objects in Python?


To concatenate a string with numbers, you need to cast the numbers to string using str(number). For example,

>>> a = "string"
>>> b = 1
>>> print a + str(b)
string1

In Python 2, you can also use backtick(``) to surround the number and get the same result with numbers and strings. Note that backticks have been removed from Python 3. For example,

>>> a = "string"
>>> b = 1
>>> print a + `b`
string1