Fix ValueError for int with base 10 error in Python

How to Fix – ValueError: invalid literal for int() with base 10

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.

valueerror invalid literal for int()

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:

📚 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.

---------------------------------------------------------------------------

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:

  1. Check the string to make sure it only contains numeric characters.
  2. If the string contains a decimal point, use the float() function instead of the int() function.
  3. 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 –

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