How to fix this ValueError invalid literal for int with base 10 error in Python

+1 vote

I am creating a program that reads a file and if the first line of the file is not blank, it reads the next four lines. Calculations are performed on those lines and then the next line is read. If that line is not empty it continues. However, I am getting this error:

ValueError: invalid literal for int() with base 10: ''.` 

It is reading the first line but can't convert it to an integer.

What can I do to fix this problem?

Nov 16, 2018 in Python by Anirudh
• 2,080 points
210,233 views

10 answers to this question.

0 votes

The following are totally acceptable in python:

  • passing a string representation of an integer into int
  • passing a string representation of a float into float
  • passing a string representation of an integer into float
  • passing a float into int
  • passing an integer into float

But you get a ValueError if you pass a string representation of a float into int, or a string representation of anything but an integer (including empty string). If you do want to pass a string representation of a float to an int, as @katyhuff points out above, you can convert to a float first, then to an integer:

answered Nov 16, 2018 by Nymeria
• 3,520 points
if you give code example, it will be easier to us.

Hello Maiul Islam Faruqi,

Here is the code as explained by @ Nymeria:

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

Hope it helps!!

Thank You!!

0 votes

Python generates the error message you present in your question whenever you call the int() builtin function with a string argument that cannot be parsed as an integer; and, in fact, the error message shows you the precise string it was trying to parse as an integer: namely ‘0.25’.

How to fix the error? It depends on what you want to do.

If what you want is to parse and convert the string to a numeric value, this particular string clearly contains a numeric representation which is not an integer but a real. The way to “fix” the error in this case is to invoke the float() builtin function, which returns a floating point (real) value. If you really wanted an integer, despite having a real in the string, use int(float(your_value_here)). Note that this converts the string to a floating point value, which is then converted to an integer via truncation—that is, by discarding the fractional part. Applying these functions to ‘0.25’ will produce a result of 0. If, on the other hand, you wanted the floating point value, just use float().

Or, perhaps, you didn’t expect the ‘0.25’. In this case, find where that string comes from and fix the problem at the origin. Can’t help you there, though, as I don’t know your code and how that string got to the int() call.

answered Mar 14, 2020 by Facts
• 140 points

Great explaination!!

0 votes

The error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that's not an integer to the int() function . In other words it's either empty, or has a character in it other than a digit. 

You can solve this error by using Python isdigit() method to check whether the value is number or not. The returns True if all the characters are digits, otherwise False .

if val.isdigit():

The other way to overcome this issue is to wrap your code inside a Python try...except block to handle this error.

Python2.x and Python3.x

Sometimes the difference between Python2.x and Python3.x that leads to this ValueError: invalid literal for int() with base 10 .

With Python2.x , int(str(3/2)) gives you "1". With Python3.x , the same gives you ("1.5"): ValueError: invalid literal for int() with base 10: "1.5".

answered Apr 29, 2020 by warrenfelsh
• 140 points

The top paragraph how do i fix my coding

Hi, @Sophie,

Could you please post your code snippet so that I can verify what wrong is happening! And what exact error you are facing.

The above-mentioned explanation has been given with different possible solutions to come out of the error.

what do you use for non integers then?

Hi, @K,

Could you please elaborate your query, what exactly you want to ask regarding?

0 votes

Hi,

Even I was going through the same problem and what I found is understanding the basics is one of the immediate hacks to come out of these kinds of errors. Here we need to understand "What does int () do in Python?". 

What the function does is, the int() function converts the specified value into an integer number. The int() function returns an integer object constructed from a number or string x, or return 0 if no arguments are given. A number or string to be converted to integer object.

answered Jul 1, 2020 by Gitika
• 65,850 points

edited Jul 2, 2020 by Gitika
this is no response but a another lesson, that don't help the asking person
Hey, @There,

If you have any proper solution then post it here. Otherwise, there is no reason to demean anyone's solution given.

I hope you understand, please post your tech queries if you have any!!!
0 votes

Hello @Everyone,

I want to bring one alternate perspective to focus on why does this error happening? What does invalid literal for int with base 10 mean in Python? 

Let's understand this first, the error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that's not an integer to the int() function. In other words, it's either empty or has a character in it other than a digit. But you get a ValueError: invalid literal for int() with base 10, if you pass a string representation of a float into an int , or a string representation of anything but an integer (including the empty string).

You can solve this error by using the Python isdigit() method to check whether the value is number or not. The returns True if all the characters are digits, otherwise False.

answered Jul 1, 2020 by Roshni
• 10,400 points
+1 vote

Hey,

I tried to look for a few other hacks from where I found my concept regarding the issue wasn't clear. I kept on trying "How do I fix ValueError invalid literal for int <UNK> with base 10?" 

My issue got resolve when I understood the applied concept where "If you still need an int and can't change the literal, try using float() to parse the string, then use int() on the result to convert it from a float to an int. This will truncate the value parsed from the string to an integer, and will avoid the ValueError as long as the string contains some real numeric value." 

answered Jul 1, 2020 by keshav
+1 vote

Just for the record:

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

Got me here...

>>> int(float('55063.000000'))
55063.0

Has to be used!

answered Aug 17, 2020 by pakainfo
• 200 points
0 votes

The error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that's not an integer to the int() function . In other words it's either empty, or has a character in it other than a digit.

answered Aug 28, 2020 by Pistle
• 1,000 points
0 votes

you use abs()

units =input('enter no:')

type (units) == str so change it useing

    units=list(units)
    for i in units:
        i=abs(int(i))
        if type(i)== int:
            if (i > 0 and i < 6):
                units = i
                print ("this is your unit size", units)
            else:
                units = 3
                print("defult units is 3")
        else:
            units = 3
            print("defult units is 3")

you enter 2 you get output is 2
                 

answered Nov 28, 2020 by Vishwa Mohandoss
0 votes
Restart the shell will be the easiest or use float() function
answered Nov 29, 2020 by Balaji

Related Questions In Python

0 votes
1 answer

invalid literal for int() with base 10 in python

Hello, You can try this simple code: def getSum(n): ...READ MORE

answered Nov 23, 2020 in Python by Niroj
• 82,540 points
125 views
0 votes
1 answer

Error is "invalid literal for int() with base 10: ' ' "

This error is caused because we try ...READ MORE

answered Oct 15, 2020 in Python by Gitika
• 65,850 points
657 views
0 votes
1 answer
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,724 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,316 views
+1 vote
1 answer

How to get the home directory using ‘~’ in Python?

Hello, @Roshni, You need to import the os module, ...READ MORE

answered Jun 26, 2020 in Python by Gitika
• 65,850 points
142 views
0 votes
5 answers

How can I use grep in Python?

The first part starts with grep , followed by ...READ MORE

answered Dec 13, 2020 in Python by Gitika
• 65,850 points
34,657 views