# Python int() Function

The int function in Python is used to convert a given value into an integer. For example:

Num = int(10.5)Str_num = int(“10.5”)

• The int function returns an integer object.
• The object is constructed from the given string or number.
• If no arguments are given then it returns 0.

### Syntax of int() function

The general syntax for using the int() function is:

class int([x])

OR

class int(x, base=10)

Where,

The x can be a number or string, bytes, or bytearray instance that represents an integer literal. The default value for the base argument is 10.

The base argument is explained further in the section below with examples.

### An example of int() using a float number

The following example shows using the int() function where x argument is float number. Have a look how it outputs the given number:

```#Python int() function demo

flt_a = int(25.5)
flt_b = int(20.3)
flt_c = int(30.7)

print (flt_a)
print (flt_b)
print (flt_c)```

The output:

25

20

30

### The example of using a string

This example uses are a string representing the integer literal. See how int() function converts string to int:

```#Python int() function with string

str_a = int("11")
str_b = int("15")
str_c = int("21")

print (str_a)
print (str_b)
print (str_c)```

The result:

11

15

21

### Using base argument in the int() function

The default value for the base argument is 10. That means using normal math i.e. 0123456789.

You may provide from 2 to 36 values. Similarly, the base 36 value uses symbols from:

0123456789abcdefghijklmnopqrstuvwxyz

The examples below show using different base values in the int() function for string to int conversion:

Using base value 36 example:

```#Python int() - base 36

bse36_1 = int('a', 36)
bse36_2 = int('z', 36)
bse36_3 = int('1', 36)
bse36_4 = int('10', 36)

print(bse36_1)
print(bse36_2)
print(bse36_3)
print(bse36_4)```

Result:

10

35

1

36

### Example of using base 16

See the output for various values given with base 16:

```#Python int() - base 16

bse16_1 = int('1', 16)
bse16_2 = int('A', 16)
bse16_3 = int('F', 16)
bse16_4 = int('10', 16)

print(bse16_1)
print(bse16_2)
print(bse16_3)
print(bse16_4)```

The output:

1

10

15

16

### Using base value 2 – binary

In this example, the base value 2 is used that represent binary.

• It has only two symbols i.e. 0 and 1.
• In the binary system, 1000 is equal to 8 in decimal.
• Similarly, 10000 is equal to 16.
• The code below shows using base value 2:
```#Python int() - base 2

binary_1 = int('1000 ', 2)
binary_2 = int('1', 2)
binary_3 = int('100', 2)
binary_4 = int('10000', 2)

print(binary_1)
print(binary_2)
print(binary_3)
print(binary_4)```

The output:

8

1

4

16

### The example of using base 8 – Octal

The octal system has eight symbols for representing all quantities. The symbols are:

0,1,2,3,4,5,6,7

The example below shows converting the string to octal:

```#Python int() - base 8

oct_1 = int('01010 ', 8)
oct_2 = int('500', 8)
oct_3 = int('7', 8)
oct_4 = int('3', 8)

print(oct_1)
print(oct_2)
print(oct_3)
print(oct_4)```

Output:

520

320

7

3

##### Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!