What is the name of Python's built-in function to convert a string to an integer?

The story behind the answer

Python’s built-in int() function converts a suitable string representation into an integer.

For example, int("42") returns the integer 42, while int("-7") returns -7. The function accepts an optional base argument, so int("101", 2) interprets the string as binary and returns 5; int("FF", 16) interprets it as hexadecimal and returns 255. Whitespace around a valid numeric string is permitted, but a string containing unsuitable characters raises ValueError.

A common misunderstanding is that int() is only for strings. It can also construct integers from numbers and other objects that provide the appropriate conversion protocol. When given a floating-point value, int() truncates toward zero rather than rounding: int(3.9) is 3, and int(-3.9) is -3. For rounding, round() is the relevant built-in.

The alternatives have different jobs. str() produces text, float() produces a floating-point number, and bool() produces True or False. Python’s int type supports arbitrarily large integers, limited mainly by available memory, although converting extremely long strings is subject to security-related limits in current Python versions.

Source: Wikipedia · fact-checked Aug. 2026

Add question to a list

Choose a list to keep this question in: