Python - int()

The int() function returns integer object from a float or a string containing digits.

>>>int(10)
10
>>>int(10.55)
10
>>>int(0.45)
0
>>>int('11')
11

To convert octal number to integer in decimal number system

>>>int('12',8)
10

Second parameter to the function is the base of number system. Here �12� is string representation of octal number that is equivalent to 10 (Ten) in decimal number system (with base=10).

Similarly, Hexadecimal number (with base=16) is converted to decimal number.

>>>int('12',16)
18

Remember that string is converted to integer only if it is made of digits only. For string containing non-digit characters, int() function encounters following error:

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

Learn Python using coding questions with answers.

Python Questions & Answers

Want to check how much you know Python?

Start Python Skill Test