The int function

The int function creates a new number from the original object. In the first parameter, we specify the object from which we will make a number. In the second optional parameter, you can specify the number format (by default, 10).

Syntax

pythonint(number or string, [number format])

Example

Let's use the int function to make a number out of a string:

pythontxt = '123'
num = int(txt)
print(num)

Result of code execution:

python123

Example

Now let's convert the string value into an octal number:

pythontxt = '0o12'
num = int(txt, base=8)
print(num)

Result of code execution:

python10

See also

  • function float,
    which creates a floating point number
  • function str,
    which creates a string
  • function list,
    which creates a list