Skip to content

Converting an integer into a string in Python

Lets say we want to do the following:

name = "John"
age = 37

print "Hello, my name is "+name+" and I am " +age+ " old"
The output will be: cannot concatenate 'str' and 'int' objects

What we are trying to do is print both a string and an integer into a single string, which does not work.  What we should do is convert the integer into a string:

name = "John"
age = 37

print "Hello, my name is "+name+" and I am " +str(age)+ " old"
Output: Hello, my name is John and I am 37 old

However this is actually expensive in terms of processing and time.  There is a better and more efficient way, see “String Substitution

Published inMayaPythonscripting