How can I concatenate str and int objects in Python?

PythonServer Side ProgrammingProgramming

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
raja
Published on 13-Dec-2017 12:34:05
Advertisements