Valueerror: Invalid literal for int() with base 10

In this post, we will see about Valueerror: Invalid literal for int() with base 10.

You will see this error, when you are trying to convert string to int using int() function. If you provide invalid string input to int() function, you will get this error.
Here is the slider to understand it better.
[smartslider3 slider=5]

Let’s see this with the help of example.

Output:

Obviously, you can not convert 'java2blog' to int type, hence getting Valueerror: Invalid literal for int() with base 10.

Let’s see another example.

Output:

30

As you can see, when we put valid int value, it worked fine.

Float number

You can directly use int() function to convert float number to int.

Output:

30

If you try to convert a float string to int using int() function, you will get Valueerror: Invalid literal for int() with base 10.
Let’s see with the help of example.

Output:

If you want to convert float string '30.23' to int, you need to first use float() function and then int() function as below.

Output:

30

As you can see, we are able to successfully convert 30.23 to int.

Use try-except

You can use try-except to handle this error.
Let’s see this with the help of example.

Output:

Can not convert java2blog to int

That’s all about Valueerror: Invalid literal for int() with base 10.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *