BlogsDope image BlogsDope

int() in Python

March 16, 2021 PYTHON FUNCTION 1401

Python deals with various data-types. Every value in Python has a data-type. Some of the basic and important data-types are: int, float, str, bool, etc.

Let's start with a simple program to accept two numbers as input from the user and multiply them:

x = input("Enter first number:")
y = input("Enter second number:")
print(x * y)

Enter first number:5 

Enter second number:6 

TypeError: can't multiply sequence by non-int of type 'str'

What is this error? We have given both the inputs of integer type, haven't we? Oh, so here lies the problem. Python interprets all the user inputs as string types. So, if we try to multiply the two inputs, we get a TypeError indicating that the inputs taken are of str type that is non-int type. To avoid such a situation, the function int() can help us. Let's see how:

What is int() in Python?

 int() is a built-in function in Python that is used to return an integer type object. It converts a given value into an integer that is a number. It takes two arguments. Let's see the syntax of it:

Syntax of int()

int(number, base)
The number/string value that has to be converted into an integer type. Default is 0
This argument is to define the base of the numerical system of the first argument - Decimal, Binary, Hexadecimal, etc. Default is base-10(Decimal)

Return Type of int()


<class 'int'>

Examples of int()

a = int()

b = "59"
b1 = int(b)


<class 'str'> 

<class 'int'> 


We can see in the first example, that with no arguments, the default value is 0. In the second example, the string value "59" is converted to an integer type value with int().

Now, let's take the first example where we started:

x = input("Enter first number:")
x1 = int(x)
y = input("Enter second number:")
y1 = int(y)
print(x1 * y1)

Enter first number:5 

Enter second number:6 


So, now we did not get any error as we converted the input value into integer type with int() which was initially a string type. You can check the data-type of all the four variables used in the example with the help of type() to understand better.

Let's take some examples with different number formats:

x = int("0o33", 8)     # 033 is octal format of decimal value 27

y = int("0xff", 16)   # 0xff is hexadecimal format of deciaml value 255

z = int("0b1010", base=2)   # 0b1010 is binary format of deciamal value 10 




Note that the second argument can be written as base=16 or just 16. Both work fine. Also, 0o, 0x,0b are prefixes in Python for Octal, Hexadecimal, and Binary formats respectively. If we do not mention the base, we will end up with a. The int() function converts the specified value into an integer number ValueError.

Let's see what is the output for float type values:

x = int(18.59)

y = int(49.5)



So, we can see that the digits after the decimal point get truncated and the value just before the decimal point is printed without any rounding off.

Can we convert a string of letters into integer type using int(). I think we may land up with an error. Let's see:

x = int("CodesDope")

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

So, as predicted we got a ValueError as it is not a valid input.

Liked the post?
Rarely seen, always noticed.
Editor's Picks

Please login to view or add comment(s).