← All coding practice

Coding Practice · Beginner · October 11, 2026 · 3 min read

Python Coding Exercise #24: Binary, Octal and Hexadecimal

Part of the series Python Practice Exercise — part 24

Before you start: Know input() and int() · Know what a built-in function is

The question

Write a program that accepts an integer in decimal form and prints it in binary, octal, hexadecimal. Use built-in functions bin, oct, and hex.

For example:

Input a number: 10
0b1010
0o12
0xa

My Solution

number = int(input("Input a number: "))
print(bin(number))
print(oct(number))
print(hex(number))

How My Solution Works

The program reads a whole number with int(input(...)) and then passes it to three built-in functions, which come with Python and need no import:

  • bin() converts it to binary.
  • oct() converts it to octal.
  • hex() converts it to hexadecimal.

Each one is printed on its own line.

What are these number systems?

The numbers we write every day use the decimal system, which has ten digits (0 to 9). Each position in a number is worth ten times the one to its right. A number system (or base) is just a different choice of how many digits to use:

SystemBaseDigits usedUsed for
Decimal100 to 9everyday counting
Binary20 and 1how computers store everything
Octal80 to 7an older shorthand for binary
Hexadecimal160 to 9, then a to fcolours (like #ff0000), memory addresses

Hexadecimal needs 16 digits, but we only have ten numerals, so it borrows letters: a is 10, b is 11, up to f, which is 15.

Why 10 becomes 1010

In binary, each position is worth double the one to its right: 8, 4, 2, 1, and so on. To write 10, you need 8 and 2, which is a 1 in the 8 and 2 positions and a 0 in the others:

Position value8421
Digit1010

That’s 1010, and 8 + 2 is 10. In octal, 10 is 12, which is one 8 plus two 1s. In hexadecimal, 10 is just the single digit a.

The prefixes: 0b, 0o, 0x

The results don’t look like plain numbers, because Python adds a prefix to show which system each one is in:

PrefixSystemExample
0bbinary0b1010
0ooctal0o12
0xhexadecimal0xa

So 0b1010 means “binary 1010”, which is 10. The prefix stops you from mixing up the numbers: without it, 10 could mean ten, two or eight.

Here are some more conversions:

Decimalbin()oct()hex()
80b10000o100x8
100b10100o120xa
160b100000o200x10
1000b11001000o1440x64
2550b111111110o3770xff

The number 255 is a good one to remember. In binary, it’s eight 1s in a row, which is the biggest value that fits in one byte (8 bits). In hexadecimal it’s just ff, which is why hex is popular for colours and data.

Things to notice

The results are strings. bin(), oct() and hex() return text, not numbers. type(bin(10)) is <class 'str'>. So you can print them or slice them, but you can’t do arithmetic with them directly. All three are explained in Built-in Functions in Python.

Removing the prefix. If you only want the digits, you can slice off the first two characters, since the prefix is always two characters long:

print(bin(10)[2:])    # 1010
print(oct(10)[2:])    # 12
print(hex(255)[2:])   # ff

(Slicing is explained in Exercise #23.) An f-string can also do the conversion with no prefix, using the letters b, o and x after a colon:

number = 255

print(f"{number:b}")   # 11111111
print(f"{number:o}")   # 377
print(f"{number:x}")   # ff
print(f"{number:X}")   # FF  (capital letters)

Going the other way

You can convert back to decimal with int(), by giving it the base as a second argument:

print(int("1010", 2))    # 10
print(int("12", 8))      # 10
print(int("ff", 16))     # 255

And in your own code you can write numbers in these systems directly, using the same prefixes:

print(0b1010)   # 10
print(0o12)     # 10
print(0xff)     # 255

All three lines print decimal numbers, because Python always shows an integer in decimal unless you convert it to a string with bin(), oct() or hex().

Key takeaways

  • bin(), oct() and hex() convert an integer to binary, octal and hexadecimal.
  • The results are strings with a prefix: 0b for binary, 0o for octal and 0x for hexadecimal.
  • Binary uses digits 0 and 1, octal uses 0 to 7, and hexadecimal uses 0 to 9 plus a to f.
  • Use slicing ([2:]) or the f-string formats :b, :o and :x to get the digits without the prefix.
  • int("ff", 16) converts a string in another base back into a decimal integer.

✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦


Practice Source: This exercise is taken from Ultimate Python Programming: Learn Python with 650+ Programs, 900+ Practice Questions, and 5 Projects by Deepali Srivastava, published by BPB Publications. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.

Tagged: python · number-systems · built-in-functions · input