Converting a string to an integer is a common task. However, you may run into errors such as “ValueError: invalid literal for int() with base 10”. This troubleshooting guide will help you overcome this problem.
Reproduce The “ValueError: invalid literal for int() with base 10“ Error
The following examples will not run without issues in Python:
Example 1:
s = 'Tutopal'
int(s)
Output:
ValueError: invalid literal for int() with base 10: 'Tutopal'
Example 2:
s = '3.1416'
int(s)
Output:
ValueError: invalid literal for int() with base 10: '3.1416'
Example 3:
s = '14a'
int(s)
Output:
ValueError: invalid literal for int() with base 10: '14a'
Example 4:
s = '2AF'
int(s)
Output:
ValueError: invalid literal for int() with base 10: '2AF'
As the error implies, Python uses this message to tell you that the strings you have provided contain invalid integer literals (as required by the int() constructor).
In particular, int() constructs an integer object from a given string or number. If no argument is provided, it returns 0. There is also an optional parameter base to indicate the numeral system based on which int() should construct the integer value.
int(x, base)
When this base parameter is set, or the x parameter isn’t a number, x has to be a byearray, bytes, or string instance that represents a valid integer literal in the provided radix base.
Python has strict lexical definitions for integer literals. But you can use common sense to detect invalid literals as well.
For instance, as ‘Tutopal‘ doesn’t represent any number, int() can’t interpret it. Meanwhile, the constructor can’t process the ‘3.1416’ string as it stores a floating-point number, not an integer. There are some digits in ’14a’, but int() isn’t designed to extract them. And finally, ‘2FA’ is a hexadecimal value, not a valid decimal integer literal.
How To Solve The Error
There is no universal fix for this problem. You must have a look at which value is given to the int() constructor and why it triggers the error. From that assessment, you can modify your program accordingly to make it run successfully.
Example 1
Since the ‘Tutopal‘ string generally doesn’t represent any numeric value in a universally accepted manner, there is no way to fix the error other than removing the int() statement. If this is an encoded string, you should use suitable functions to decrypt it.
Example 2
Instead of int(), use the float() constructor to convert the string to a floating-point number:
s = ‘3.1416’
n = float(s)
print(n)
print(type(n))
Output:
3.1416
<class 'float'>
You can learn more about converting strings to floating-point numbers in Python here.
Example 3
You can use a regular expression to find digits in a string:
import re
s = '14a'
n = re.findall(r'\d+', s)
print(int(n[0]))
Output:
14
The re module provides the findall() function, which can find every match of a certain regular expression. In this case, the ‘\d+’ expression matches all the digit characters. We then use the int() constructor to convert the returned array to an integer object.
Example 4
Since ‘2AF’ represents a number in hexadecimal, not decimal, you can’t use the default base parameter value (10) with int(). Change it to 16, the base number of that numeral system and int() shouldn’t raise the ValueError exception anymore:
s = '2AF'
n = int(s, base=16)
print(n)
print(type(n))
Output:
687
<class 'int'>
The code above takes the integer literal ‘2AF’ and converts it to an integer object in decimal, which has the value of 687.
Summary
The int() constructor can help create an integer object in Python from a string.
But if the given string doesn’t contain a valid integer literal, you will run into the “ValueError: ValueError: invalid literal for int() with base 10”. Fixing the issues with the string or using proper functions can help you get rid of this error message.
Reference Sources: