When programming in Python, avoid “TypeErrors” by transforming an integer to a string. For example, by converting numbers into strings you can easily align the outcomes into a table. Another example is to concatenate a number prior to a string to identify a product. You use the “str” function to transform an integer to a string.
Converting Int to String in Python
- Open your Python editor.
- Type “str (number)”
- Press “Enter”. This runs the string function to convert an integer to a string.
- In the example below, you trigger the user to enter an number. Use the “int” function to convert the number to an integer. Add 5 to the integer. Then, the “str” function converts the integer to a string so that Python can concatenate and print out the response.
- “print (“Get in an integer:”,) response = input ()
- number = int (answer) addFive = number +5
- print (“Adding 5 to your number, we get the response” +str (addFive))”
- If you do not use “str” to transform the integer to a string, Python will provide you the mistake “TypeError: can not concatenate “str” and “int” things”.