Integer literal

Explanation of integer literals in Python and how they define numeric bases through prefixes like 0b, 0o, and 0x

« Back to Glossary Index

An integer literal is a direct representation of an integer value written in the source code. In Python, integer literals can specify different numeric bases through prefixes:

  • 10 → decimal (base 10)
  • 0b100 → binary (base 2)
  • 0o11 → octal (base 8)
  • 0x1F → hexadecimal (base 16)

When using int(string, base=0), the argument base=0 tells Python to interpret the base of the string automatically, following the same rules as integer literals. For example:

>>> int('0b100', base=0)
4
  

This behaviour allows Python to recognise prefixes like 0b, 0o, or 0x and convert the string to the corresponding integer value.

« Back to Glossary Index