Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
integer
communitycreator

What is the int() function in Python?

Rameen Farrukh

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Python int() function is a built-in function that converts a specified value into an integer number.

For example, if you have a string, x, the int() function would return an integer object constructed from the x string.

Syntax

int(value, base)

Parameters

value: A number or string to be converted to integer object.

base: Optional, with default value 10. Number format.

Code

How to convert a string into an int:

value= '187'
print('converting string 187 into an int:')
print int(value)
string = '10'
print('printing string 10 with base 2,4,8 and 16:')
print(int(string, 2))
print(int(string, 4))
print(int(string, 8))
print(int(string, 16))
string = '100'
print('printing string 100 with base 2,4,8 and 16:')
print(int(string, 2))
print(int(string, 4))
print(int(string, 8))
print(int(string, 16))

How to convert a float into an int:

print('converting float 133.5 into an int:')
x = int(133.5)
print(x)

RELATED TAGS

python
integer
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring