How to Convert an int to string in Python?

As writing the code in Python, there will be scenarios when you will need to convert integers into a string. You may use the str() function for converting int into the string.

For example, consider we have a variable containing an int value:

int_num = 123

This is how you may convert this int number to a string:

print (str(int_num))

See code and output

Video Tutorial of string to int Python

Another example can be, if you need to concatenate a number to a string then you first need to convert that number into the string by using the built-in method.

Use Python str() method for converting integer to a string

The Python provides the str() method to convert integers into strings.

The str() method takes a parameter that can be an integer, float etc. See the examples below for learning how to convert a number into the string.

An example of Python str() method

In this example, two string variables are declared. Another variable which is an int type is declared and assigned a value.

Finally, the print() function is used where string variable is concatenated with the int variable. The int() variable is used in the str() method as shown below:

See this example online

The output will be:

This is Python Tutorial. 123

What happened if int was directly used?

If no str() method was used in above example, the 123 was taken as int and an error would have produced. See the code and output below:

See this example online

The output of the above code will be an error as shown in the graphic below:

An example of float to string conversion

Similarly, you may use the str Python str() function for converting a float to a string. See this simple example:

The output:

Python float to string example 421.25

If no str() function was used in the above example, the following error would have occurred:

print (str1 + ” ” + a_float)

TypeError: Can’t convert ‘float’ object to str implicitly

An example of list of mixed objects

Let us say, you need to concatenate Python list items into a string. Although, there are other ways of doing this like using the join method.

You have no idea about the type of list items. For example, it may contain strings, int, float numbers etc.

See the following code snippet where all list items (int, float and even strings etc) will be converted to strings and a final result will be a concatenated string.

The output:

This is 1 Python 3.52 str

You noticed, the int is converted to string along with a float item. If a string is given to the str() method, it returns the string. So, the end result is a concatenated string based on the mixed list items.

Using join method with str()

Similarly, you may use the simple and efficient way to concatenate a sequence items that contains int or float numbers. See this example of using join method where str() method is used for converting the list items:

The output of above code is:

Python convert 15.5 by str 60

Note: You may use other sequences like tuple there as well.

Leave a Comment