Python - Hexadecimal, octal, and binary literals


Integers may be coded in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2).

Hexadecimals start with a leading 0x or 0X, followed by a string of hexadecimal digits (0-9 and A-F).

Hex digits may be coded in lower- or uppercase.

Octal literals start with a leading 0o or 0O (zero and lower- or uppercase letter o), followed by a string of digits (0-7).

In 2.X, octal literals can also be coded with just a leading 0, but not in 3.X.

This original octal form is replaced by the new 0o format, which can also be used in 2.X as of 2.6.

Binary literals begin with a leading 0b or 0B, followed by binary digits (0-1).

All of these literals produce integer objects in program code; they are just alternative syntax for specifying values.

The built-in calls hex(I), oct(I), and bin(I) convert an integer to its representation string.

int(str, base) converts a runtime string to an integer per a given base.

Related Topics