Sep 1, 2023 4 min read

How to Convert Integer into String in Python

Convert Integer into String in Python with our step-by-step tutorial. Strings are characters, integers are whole numbers in Python.

Convert Integer into String in Python
Table of Contents

Introduction

Before we begin talking about how to convert integer into string in Python, let's briefly understand what is integer and string in Python?

A Python integer is a whole number with no fractional part and unlimited precision, e.g. 0, 100, -10 and are commonly used for arithmetic operations like addition, subtraction, and multiplication. They are useful in various applications, including counting, indexing, and storing IDs or quantities.

A string is a sequence of characters. Among Python types, str stands for strings. They are employed for tasks like displaying output, storing names or sentences, and interacting with users. Strings can be combined (concatenated) or modified using functions like splitting, replacing, or converting to uppercase or lowercase.

Several data types are available in Python. Python code will sometimes require you to convert one data type to another. As an example, if you want to concatenate a string and integer, you'd first have to convert the integer into a string.

In this tutorial, you will learn how to convert an integer into a string in Python. We will also address a few FAQs on how to convert an integer into a string in Python.

Python str() Function

Using the built-in str() function, we can convert integers and other data types to strings in Python.

A given object is converted into a string using the str() function. There are several forms this function can take:

class str(object='')
class str(object=b'', encoding='utf-8', errors='strict')
  • object - The object is to be converted into a string.

You usually pass only one argument (object) to the function when converting an integer to a string, although the function accepts three arguments.

Converting a Python Integer into String

By passing 23 to the str() function, you will get the string version:

str(23)
type(days)
Output

'23'
<class 'str'>

23 is an object of string type because of the quotation marks around it. It is also evident from the type() function that the object has the string type.

💡
When declaring strings in Python, single quotes ('), double quotes ("), or triple quotes (""") are used.

Concatenating Strings and Integers

Here's a quick example of concatenating strings and integers using the + operator:

number = 6
lang = "Python"
quote = "There are " + number + " relational operators in " + lang + "."
print(quote)

Due to Python's inability to concatenate strings with integers, a TypeError exception will be thrown:

Output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

Strings can be created by passing the integer to the str() function as follows:

number = 6
lang = "Python"
quote = "There are " + str(number) + " relational operators in " + lang + "."
print(quote)

As a result, when you run the code, it will be successfully executed:

Output

There are 6 relational operators in Python.

Concatenating strings and numbers can also be done in different ways.

The format() function of the built-in string class formats a string using an arbitrary collection of positional and keyword arguments:

number = 6
lang = "Python"
quote = "There are {} relational operators in {}.".format(number, lang)
print(quote)
Output

There are 6 relational operators in Python.

You can use f-strings that are literal strings with a prefix f and contains an expression inside braces:

number = 6
lang = "Python"
quote = f"There are {number} relational operators in {lang}."
print(quote)
Output
There are 6 relational operators in Python.

Also, you can use the old %-formatting:

number = 6
lang = "Python"
quote = "There are %s relational operators in %s." % (number, lang)
print(quote)
Output

There are 6 relational operators in Python.

FAQs to Convert Integer into String in Python

What happens if you try to convert a non-integer value to a string? 

Python will raise a TypeError if you attempt to convert a non-integer value, like a float or a string, using the str() function.

Is there an alternative method to convert an integer to a string? 

Yes, you can also use formatted string literals (f-strings) to achieve this. Simply enclose the integer within curly braces inside a string preceded by an 'f'.

Can you convert a negative number into a string? 

Absolutely! The str() function works equally well for negative numbers. It will convert the negative integer into a string representation.

What if the integer contains leading zeros? Will they be preserved when converting to a string?

No, leading zeros are not preserved when converting an integer to a string. The string representation will not include any leading zeros.

Is it possible to convert a string containing digits into an integer and then convert it back to a string? 

Yes, Python allows you to convert a string of digits to an integer using the int() function, and then convert it back to a string using str().

How can I remove any extra spaces while converting an integer to a string? 

To remove extra spaces, you can utilize the strip() method on the converted string. It will eliminate any leading or trailing spaces.

Can we convert large integers into strings in Python? 

Yes, Python can convert large integers (even those beyond the range of sys.maxsize) into strings without any issue.

Conclusion

We hope this detailed guide helped you convert integer to string in Python.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.