In Python, the ValueError: invalid literal for int() with base 10
error occurs when you try to convert a string that cannot be converted to an integer using the int()
function. This error can be frustrating, especially if you are not sure what is causing it. In this tutorial, we will explore the reasons why this error occurs and how to fix it.
Understanding the error
The ValueError: invalid literal for int() with base 10
error occurs when you try to convert a string to an integer using the int()
function, but the string cannot be converted to an integer. Here are some common scenarios in which this error occurs:
- The string contains non-numeric characters.
- The string contains a decimal point.
- The string is empty.
Let’s now look at examples of the above scenarios.
# string contains non-numeric characters string = "abc123" integer = int(string) print(integer)
Output:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[1], line 3 1 # string contains non-numeric characters 2 string = "abc123" ----> 3 integer = int(string) 4 print(integer) ValueError: invalid literal for int() with base 10: 'abc123'
Here, the string that we’re trying to convert contains non-numeric characters and thus we get the ValueError: invalid literal for int() with base 10: 'abc123'
.
# string contains a decimal point string = "12.34" integer = int(string) print(integer)
Output:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[2], line 3 1 # string contains a decimal point 2 string = "12.34" ----> 3 integer = int(string) 4 print(integer) ValueError: invalid literal for int() with base 10: '12.34'
In the above example, we used a string representing a floating point value. Since this string cannot be converted to an integer, we get the ValueError
.
# the string is empty string = "" integer = int(string) print(integer)
Output:
Introductory ⭐
- 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
Intermediate ⭐⭐⭐
- 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
Advanced ⭐⭐⭐⭐⭐
- 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.
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[3], line 3 1 # the string is empty 2 string = "" ----> 3 integer = int(string) 4 print(integer) ValueError: invalid literal for int() with base 10: ''
An empty string also cannot be converted to an integer.
How to fix the error
Here are the steps to fix the ValueError: invalid literal for int() with base 10
error:
- Check the string to make sure it only contains numeric characters.
- If the string contains a decimal point, use the
float()
function instead of theint()
function. - Check the string to make sure it is not empty.
Let’s now revisit the examples from above and fix them.
1) Make sure that the string contains only numeric characters
# string contains only numeric characters string = "123" integer = int(string) print(integer)
Output:
123
We don’t get an error here.
2) If the string contains a decimal point, use float()
instead
# string contains a decimal point string = "12.34" float_number = float(string) integer = int(float_number) print(integer)
Output:
12
In the above example, we have used the float()
function to convert the string to a float, and then used the int()
function to convert the float to an integer.
3) Make sure that the string is not empty
# string is empty string = "" if string: integer = int(string) print(integer) else: print("String is empty")
Output:
String is empty
Here, we have added a check to make sure the string is not empty before converting it to an integer. You can see that we don’t get an error here.
Conclusion
The ValueError: invalid literal for int() with base 10
error occurs when you try to convert a string to an integer using the int()
function, but the string cannot be converted to an integer. This error can be fixed by checking the string to make sure it only contains numeric characters, is not empty, and does not contain a decimal point. If the string contains a decimal point, use the float()
function instead of the int()
function.
You might also be interested in –