ValueError: invalid literal for int() with base 10 error occurs when you try to pass a string value to the int() function or treat strings as integers. Anything that is inside the quotation marks in Python is treated as a string so even if you have a number but it is an inside quotation, then it is a string. In this short article, we will learn how to solve ValueError: invalid literal for int() with base 10 error using various methods. Moreover, we will also discuss how we can understand python errors.

ValueError: invalid literal for int() with base 10
The error ValueError: invalid literal for int() with base 10 occurred when we passed a string value to the integer function. A string containing numbers can be converted to integers values using the int() method but if the string contains characters, then we will get an error.
For example, let us assume that we have the following Python code:
# defining Python
string = "This is Bashir"
# applying to int function
a = int(string)
Output:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipykernel_329137/896642843.py in <module>
3
4 # applying to int function
----> 5 a = int(string)
ValueError: invalid literal for int() with base 10: 'This is Bashir'
Even if the string itself is a floating number, still you will get the same error. For example, see the following code:
# string number
num = '23.7'
# applying int() method
a = int(num)
Output:

As you can see, we still got the error because the dot point in the floating number is treated as a character rather than a decimal point when it is a string.
If we will pass a number without any floating point, then we will not get the error anymore:
# string number
num = '237'
# applying int() method
a = int(num)
When you run the code, you will not get any error anymore because this time the int() method will convert the string to an integer value.
Solution-1: Using try-except block
The try-except block is used to handle errors in Python. We can use the try-block block to handle ValueError: invalid literal for int() with base 10 error. The block inside the try block will be executed first and if any error occurs then instead of crashing the script the alternative code inside the except block will be executed as shown below:
# string number
num = 'string value'
# try block
try:
# applying int() method
a = int(num)
# except block
except:
print("There is an error!!!!")
Output:
There is an error!!!!
As you can see, even the strings cannot be converted to int, but still the script was not crashed, because we handled the error using a try-except block.
Solution-2: Using float() method
If the error is because of floating points then you can first convert the string value into a floating point and then apply it to the int() method.
Sometimes, we might have a string that contains floating numbers and still we cannot directly apply the int() method on it because the point in the string is different from the decimal point. Let us take an example and see:
# string
string = '3.43'
# converting to floating point
f = float(string)
# applying to int method
i = int(f)
As you can see, when we run the program, it will not give any error.
Solution-3: Using filter() and isdigit() method
The is.digit() method returns true if the string has numeric values. For example, see the following examples:
# strings
num1 = 'abced'
num2 = '234'
num3 = '4.43'
# printing
print(num1.isdigit())
print(num2.isdigit())
print(num3.isdigit())
Output:
False
True
False
As you can see, we got True for only the integer values.
Let us now use the filter function along with isdigit() method to solve ValueError: invalid literal for int() with base 10.
# string
string = '23bashir24'
# filtering
num= int(''.join(filter(str.isdigit, string)))
# printing
print(num)
Output:
2324
Notice that we have filtered and extracted only the numeric values from the string.
Solution-4: Using if-else statements
Another way to handle ValueError: invalid literal for int() with base 10 error is to use if else statements. We can check if the string has numeric values then we will apply the int() function and if not, then we will skip it:
# string
string ="Bashir"
# if statement
if string.isdigit():
num = int(string)
# else statement
else:
print("the string has no numeric values")
Output:
the string has no numeric values
As you can see, we didn’t get the error and the program run successfully without giving any error:
Understanding the error
In Python, the error usually gives a lot of information and can be easily solved. The errors usually have two parts. The first part represents the category of the error. In this case, the category of the error is ValueError. The second part of the error gives more specific information about the error for example it says invalid literal for int() with base 10 which means there is a problem with the argument that we have passed to the int() method.
What is ValueError in Python?
When a function or operation receives an argument with the correct type but the wrong value, a ValueError is generated. A ValueError will be generated, for instance, if you try to convert a string to an integer but it contains characters that can’t be translated into numbers. This may occur when the input data is invalid for some other reason, or when it is not in the proper format. A ValueError can be handled by using a try-except block to catch the error and take a different course of action, or it can be avoided by validating the input to ensure that it is in the right format before executing the operation.
Reasons for getting ValueError: invalid literal for int() with base 10 error
Now we already know how to solve the error but if you are still facing the error then you might be facing the error because of some other reasons. There are a few common reasons for getting this error:
- When you are trying to convert string values to integer values when the string does not have to contain any numeric values.
- It can also occur when you tried to convert an empty string to a numeric value using the int() method.
- It can also occur when you tried to convert a string into an integer that contains too large or too small numeric values.
- You will face the same error when you try to convert a non-string to an integer value.
Summary
In this short article, we discussed how we can solve ValueError: invalid literal for int() with base 10 error. The error occurs when we tried to convert a string value into an integer using the int() method. Here, we discussed four different methods to solve ValueError: invalid literal for int() with base 10 error. Moreover, we also discussed some of the common reasons for getting this error:
Related Errors
- Pandas groupby() examples with source code
- How to pretty-print JSON columns in Python? Examples
- Python os join path – 5 Examples