ValueError: Invalid literal for int() with base 10

Python generates the ValueError: invalid literal for int() with base 10 when you pass an unformatted number in string value as an integer in the int() function. Python is a very versatile programming language. It …

Invalid literal for int() with base 10

Python generates the ValueError: invalid literal for int() with base 10 when you pass an unformatted number in string value as an integer in the int() function.

Python is a very versatile programming language. It always provides easiness to the programmers. Python is known to convert one data type value to another data type value. 

You can convert integer to string, a string to integer, float to integer, and many more.

In other words, you can say that the invalid literal for int() with base 10 when you are passing a string in the int() function that is not converted to an integer. This also indicates that either you are passing an empty string as a parameter to the function or the object is invalid.

What is ValueError: 

In Python, value error generates when you try to pass an inappropriate value to a variable of a function. 

For example ValueError: invalid literal for int() with base 10.

Python int() method: 

Int() is a built-in function of Python. We use the int() method when we want to convert a number or a string value into an integer. 

Syntax: int(string,base) or int(number)

print(int(10))

Output:

10
print(int('13',10))

Output:

13

The int() function returns a number that is converted from a string or integer.

If you leave the field empty, it will return 0.

print(int())

Output:

0

The default base on int() function is 10 the other bases are 2, 4, 8, and 16.

ValueError: invalid literal for int() with base 10 in Python:

Python is well known for converting the value of one data type to another data type. 

The int() built-in function of python helps us to convert a numeric or a string formatted as a number value into an integer.

The ValueError: invalid literal for int() with base 10 occurs when you pass a non-digit object of a decimal number system. That means you are trying to input an inappropriate or unacceptable value as a parameter on int(). 

Hence, it is producing a valueError and that you are converting an invalid object into an integer so it produces the invalid literal for int() with base 10.

Example:

print(int("hello"))

Output:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'hello'

Causes of valueError: invalid literal for int() with base 10 in Python:

The main aim to use int() function in python is that to convert a number formatted as a string into an integer. 

So, whenever you pass any argument that is not a digit of a decimal number system or a string or a string with no integers then the ValueError: invalid literal for int() with base 10 generates. 

Let’s understand with the help of the following examples.

Example 1:

print(int('2.5'))

Output:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ‘2.5’

Example 2: Passing a string value

print(int("hello"))

Output:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'hello'

Example 3: Passing a float inside string

print(int("20.1"))

Output:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '20.1'

Example 4: Passing a list inside a string

print(int("[4]"))

Output:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '[4]'

Example 5: Passing a tuple inside string

print(int("(2)"))

Output:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '(2)'

Example 6: Passing a dictionary inside string

print(int("[2:40]"))

Output:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '[2:40]'

Example 7: Passing an empty string

print(int(""))

Output:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ' '

How to solve ValueError: invalid literal for int() with base 10 in Python:

As we read earlier that in python the valueerror invalid literals for int() with base 10 generates when passing an invalid argument in the int() function.

We also know that the int() function only accepts the number formatted strings or integer with a decimal number system as its parameter.

At a beginner level, you can fix this error by taking preemptive measures. It means that you can check that input you are passing as the parameter is an integer or not. 

Following are the proactive approaches for avoiding invalid literal for int() with base 10 error.

1. Use the float to numbers with decimals:

When the python compiler reads your code and at a point where you input a number with decimal as a parameter of int() function, it will generate the invalid literals for int() with base error. 

To convert the number with a floating-point as input in int() function, it is a good approach to check whether the number is integer or float that means check if the number contains “.” in it or not.

If your input number contains a decimal point”.” in it then you should first convert the input number as floating-point. 

Example:

flo = "2.5"
print(int(float(flo)))

By using a float in the code above, we convert the string “2.5” to floating-point. after converting it to the floating-point number we convert it into integer “2” by the help truncation technique.

The truncation technique helps to discard the fractional part from a number.

If you want the floating-point value as output use the float() function.

Example:

print(float('2.5'))

2. Use the indigit() function:

The indigit() function of python helps to find out whether the input of the function only consists of digits or not. 

In the int() function, if the parameter you passed consist of input string of a formatted digital number, the function will return “True” else it returns “False” as the output.

Example:

num = "2.5"
if num.isdigit():
  print(int(num))
else:
  print("input value is not an integer digit")

Output:

input value is not an integer digit

Example 2:

In this example let’s input a character other than digit like alphabets. Because the input is not a digit, the compiler will give false as output.

num = "hello"
if num.isdigit():
  print(int(num))
else:
  print("input value is not an integer digit")

Output:

input value is not an integer digit

3. Use try-except exception handling method:

The valueerror: invalid literals for int() with base 10 can also be solved by using exception handling to handle this valueerror. 

There are two blocks present in the code of exception handling try and except. The code is executed in the try block. Whenever any error occurs it will generate in the try block. The except block is used to handle the error.

It will show a message to the user so that the user will have knowledge about the exception. 

Example:

x ='noninteger'
try:
  int(x)
except:
  print('Can not convert', x ,"to int")

In the above code, we input a set of alphabets (non-digit characters) as the parameter of the int() function. The output will generate an exception that can be handled by the except block.

Output:

can not convert noninteger to int

Conclusion: 

The ValueError: invalid literal for int() with base 10 generates when you try to convert a non-formatted string as an integer. This error will generate when the parameter passed inside the int() function is not a number from a decimal system. 

Valueerror: invalid literal for int() with base 10 in python can be solved by using the float() method to solve the floating point problem. Exception handling is also a good solution to this error. 

Using indigit() built-in function of python is also a good solution to fix the invalid literal for int() with base 10 error.

This article has a piece of complete information on what is this error, its causes, and all the solutions in detail.

Suggested Read: Remove Brackets from list Python

Leave a Comment