How to Convert Int to String in Python

When programming in Python, avoid "TypeErrors" by converting an integer to a string. For example, by converting numbers into strings you can easily align the results into a table. Another example is to concatenate a number before a string to enumerate an item. You use the "str" function to convert an integer to a string.

Step

Launch your Python editor.

Step

Type "str (number)".

Step

Press "Enter". This runs the string function to convert an integer to a string.

Step

In the example below, you prompt the user to enter an number. Use the "int" function to convert the number to an integer. Add five to the integer. Then, the "str" function converts the integer to a string so that Python can concatenate and print out the answer.

Step

"print ( "Enter an integer: ",) answer = input ()

Step

number = int (answer) addFive = number +5

Step

print ( "Adding 5 to your number, we get the answer " +str (addFive))"

Step

If you do not use "str" to convert the integer to a string, Python will give you the error "TypeError: cannot concatenate "str" and "int" objects".