convert integer to string in python

Python – Convert Integer to String

In this tutorial, we will look at how to convert an integer (int datatype) in Python to a string with the help of some examples.

Integer as string in Python

Whole numbers in Python are generally stored using the int type and in some cases as a string. For example, “150” is a string containing the number 150. You can use the string isdigit() function to check if a string represents a number or not.

# string storing a whole number
s = "150"
# check if string contains numeric value
print(s.isdigit())

Output:

True

With numbers stored as strings, you cannot apply numeric computations to those string numbers. For example, if you add “150” and “50” using the + operator, it will be treated as a string concatenation operation resulting in “15050” and not “200”.

# addition of numeric values
print(150+50)
# addition of numeric string values
print("150"+"50")

Output:

200
15050

Thus, a common use case for storing numbers as strings is when you don’t need to perform arithmetic computations on the number. For example, storing numeric Id values as strings or when you want to perform string operations such as slicing, etc. on the numbers.

How to convert int to string in Python?

convert integer to string in python

You can use the Python built-in str() function to convert an integer to a string in Python. The following is the syntax –

# convert integer i to string
str(i)

It returns the str version of the passed object. str is the default string class in Python.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Example

Let’s look at some examples of using the above syntax to convert an integer value to a string.

Here we apply the str() function on an integer value 150.

# integer value
num = 150
# convert int to string
s = str(num)
# display the string and its type
print(s)
print(type(s))

Output:

150
<class 'str'>

You can see that we get 150 as a string.

You might also be interested in –

  1. Extract Numbers From String in Python
  2. Python – Check if String Contains Only Numbers
  3. Python Tuple to Integer


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top