Back to Blog Page
Programming

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

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

Written by Kolade Chris | Dec 26, 2022 | #Python | 2 minutes Read

In Python, you can convert one data type to another. For instance, integers to strings and vice versa, floats to strings, and many others.

However, one conversion Python won’t allow you to do is to convert a string structured as a floating point number (a number with a decimal point) to an integer. For example, converting “5.20” to an integer. This results in the ValueError: invalid literal for int() with base 10: error.

1
print(int("5.20"))
2
3
# Traceback (most recent call last):
4
# File "value_error.py", line 1, in <module>
5
# print(int("5.20"))
6
# ValueError: invalid literal for int() with base 10: '5.20'

This error also occurs if you try to convert a string of letters to an integer:

1
print(int("Python"))
2
3
# Traceback (most recent call last):
4
# File "value_error.py", line 1, in <module>
5
# print(int("Python"))
6
# ValueError: invalid literal for int() with base 10: 'Python'

In this article, you’ll learn about how to fix this error in Python so you can avoid it in the future.

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

What causes this error is already in the error message – the int() method expects an integer. Any number with a decimal point is not an integer. Hence, such a number is not in base 10.

This means you cannot convert a floating point number formatted as a string to an integer with the int() method.

How do I fix this ValueError invalid literal for int with base 10 error in Python?

To fix this error, Python provides a method for converting such numbers formatted as a string – the float() method. So, whenever you have a number with a decimal point you want to convert, you should use the float() method instead of int().

1
print(float("5.20"))
2
3
# Output 5.2

If you want the string as an integer, you can use the combination of the int() and float() methods – convert the number to a float first, then convert it back to an integer:

1
num = "5.20"
2
# convert the number to float
3
floated_num = float(num)
4
5
# convert the floating point number to an integer
6
intNum = int(floated_num)
7
8
print(intNum)
9
# 5

Here’s how to do it all in one line:

1
print(int(float("5.20")))
2
# 5

You can also use try...except… to prevent this error. If the string cannot be converted to an integer, the except block will run:

1
num = '128.4'
2
3
try:
4
res = int(num)
5
except ValueError:
6
res = 0
7
8
print(res)
9
# 0

If the string can be converted to an integer, the try block will run:

1
num = '128'
2
3
try:
4
res = int(num)
5
except ValueError:
6
res = 0
7
8
print(res)
9
# 128

Subscribe to my newsletter for coding tips, videos from reputable sources, articles from OG tech authors, and a ton of other goodies.

No BS. No fluff. Just pure software development goodies on a Sunday every week.