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.
📚 Discover Online Data Science Courses & Programs (Enroll for Free)
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
# string storing a whole number s = "150" # check if string contains numeric value print(s.isdigit())
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")
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?
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.
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
# integer value num = 150 # convert int to string s = str(num) # display the string and its type print(s) print(type(s))
150 <class 'str'>
You can see that we get
150 as a string.
You might also be interested in –
- Extract Numbers From String in Python
- Python – Check if String Contains Only Numbers
- 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.