In this tutorial, we will learn about python int() method and its uses with examples.
The int() is the built-in method of python, which converts any number or string objects into an integer object; it is also used for typecasting the integer number.
The syntax of int() is:
int() method takes two parameters as an argument:
- value – A number or string object which wants to convert into an integer number. By default, it takes zero as an argument.
- base – A base number that represents the number format. The default value is 10.
Let us see some examples of int() methods in python.
Example 1: How to use the int() method in python?
Example 2: How int() works for decimal, octal, and hexadecimal?
It will cause an “invalid literal for int()” error when using strings with non-numerical values.
The output will be as follow:
Example 3: Taking input as integer and float in input() method.
The output will be as follows.
Rules of int() method
- an integer object from the given number or string treats the default base as 10
- (No parameters) returns 0
- (If base given) treats the string in the given base (0, 2, 8, 10, 16)