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

avatar

Borislav Hadzhiev

Last updated: Apr 24, 2022

banner

Photo from Unsplash

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

The Python "ValueError: invalid literal for int() with base 10" occurs when we pass a string that cannot be directly converted to an integer (e.g. an empty string or a float) to the int() class. To solve the error, convert the string to a float first, e.g. int(float(my_str)).

python valueerror invalid literal for int with base 10

Here are 3 examples of how the error occurs.

main.py
# ⛔️ ValueError: invalid literal for int() with base 10: '5.0' print(int('5.0')) # ⛔️ ValueError: invalid literal for int() with base 10: '' print(int('')) # ⛔️ ValueError: invalid literal for int() with base 10: 'a2c3' print(int('a2c3'))

We can't directly pass a string that contains a float to the int() class, because the class expects a value with base 10 (where each digit can have an integer value ranging from 0 to 9 - 10 possibilities).

To solve the error, convert the string to a float before passing it to the int() class.

main.py
result = int(float('5.0')) print(result) # 👉️ 5 print(type(result)) # 👉️ <class 'int'>

The float() class takes care of converting the string to a floating-point number, and the int() class converts the float to an integer.

Make sure to print() the value you are passing to the int class as it may not contain what you expect.

For example, you might be passing an empty string or a string that contains a space to the int() class.

main.py
val = ' ' result = int(val.strip() or 0) print(result) # 👉️ 0

If removing all leading and trailing whitespace from the string returns an empty string, we pass 0 to the int() class.

You can also use a try/except block to handle the error.

main.py
val = 'abc' try: result = int(val) except ValueError: result = 0 print(result) # 👉️ 0

If calling the int() class with the value raises a ValueError, the except block runs where we set the result variable to 0.

If you have a string that may also contain characters but you only need to extract an integer, use the filter() function to filter out all non-digits.
main.py
my_str = 'a40b' my_num_1 = int(''.join(filter(str.isdigit, my_str))) print(my_num_1) # 👉️ 40

The filter function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.

The str.isdigit method returns True if all characters in the string are digits and there is at least 1 character, otherwise False is returned.

We basically check if each character in the string is a digit, return the result and join the digits into a single string before using the int() class to get an integer value.

Conclusion #

The Python "ValueError: invalid literal for int() with base 10" occurs when we pass a string that cannot be directly converted to an integer (e.g. an empty string or a float) to the int() class. To solve the error, convert the string to a float first, e.g. int(float(my_str)).

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.