How to Convert Int to String in Python

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.
The Python programming language gives you a few different ways to convert an integer or another type of number to a string.
Image Credit: skynesher/E+/GettyImages

The Python programming language gives you a few different ways to convert an integer or another type of number to a string. You can do this with the "str" function, or "unicode" function if you are working in Python 2 and want a Unicode string, or with format strings. If you want to go the other way, it's also possible to convert a string containing an integer to that integer.

Advertisement

Convert with the Str Function

Video of the Day

The built-in Python function "str" can convert an integer, a floating-point number or many other types of data to a human-readable and printable string. To use it, simply call it on a number anything else, as in str(5), which will give you the string "5." If you wish to call it on a variable containing a number, do so in the same way, such as str(x).

Advertisement

Video of the Day

If you're using Python 2.7 or an earlier version, you can also use the "unicode" function to generate a Unicode string from most types of data, including integers. Unicode is an international standard for storing characters from lots of different languages, along with special characters such as zodiac symbols and emoji. Calling unicode(5) will give you the Unicode string u'5'. This function isn't necessary in Python 3, where strings are Unicode by default.

Advertisement

Using a Format String

Another option for converting an integer to a string is to use a format string. A format string is simply a string of text with placeholders for where you want to put other data, each with the form "{}." Call the format string's format method with arguments in order for each placeholder, such as "The number {} is greater than {}".format(6,2).

Advertisement

This is especially useful if you want to insert numerals or other data into larger strings, but you can also use a format string with just one placeholder if you prefer, such as "{}."

You can also refer to the arguments in a format string in numeric order starting with "{0}" or by name if you use named arguments. For instance, the format expression "The number {0} is greater than {1}, but {0} is less than {largest_num}".format(6,5,largest_num=7) will yield the string "The number 6 is greater than 5, but 6 is less than 7.

Advertisement

Advertisement

Format strings work in Python 2 and Python 3.

Python String to Integer Function

In some cases, you may want to go the other way, from Python string to int. To do this, call the built-in "int" function on a string containing the written representation of an integer, such as int("2"), which yields 2. If the string contains anything besides an integer, this will fail and generate an exception. This is true even if there's an integer at the start of the string or within it, although you can take a substring of the string or use a regular expression to find the integer.

Advertisement

Similarly, you can have Python convert a string to a double, meaning what's called a double-precision floating-point number, using the float function. For instance, float("3.2") = 3.2. Keep in mind that floating-point numbers have limited precision, so you may not get exactly the number written in the string. Use the decimal class if you need arbitrary precision. Read the definitions of float and decimal correctly to see what suits your needs best.

Advertisement

Python Float to Int Options

Sometimes, you may wish to convert a floating-point number to an integer. If it is already an integer, you can use the int function, so int(3.0) = 3. If it is not already an integer, the function will drop what's after the decimal point, rounding the number down.

You can also use the built-in round function to round to the nearest integer or math.ceil to round numbers to the next highest integer.

Advertisement

Advertisement

references