}

How to convert integer to string in Python?

Created:

Introduction

Converting an integer to string in python is straighforward to do it. If you are a beginner software developer you could have errors like:

AttributeError: 'int' object has no attribute 'str'

At the end of this article we show a common wrong code.

Converting integer to string

If you wan to convert the integer 30 to string, just use str()

str(30) 

The code above will return the string '30'.

You can use the str() function with variables:

tutorials = 30
str(tutorials) 

You can also use str() with other type of objects. If you have a custom object and you want a particular output when use str(), you need to implemented __str__()

Common errors

Developer that come from another lenguage could try to execute the following code:

test = 30
test.str()

This will return a common error message:

AttributeError: 'int' object has no attribute 'str'

How str() works

When you call str(object) the str() function will call the magic method __str__(). The __str__() method is also called when you print(x).

More information

Check official python documentation about int() and str() here: