Print the binary representation of a Number in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
4 min

banner

# Table of Contents

  1. Print the binary representation of a Number in Python
  2. Convert an integer to binary and keep leading zeros

# Print the binary representation of a Number in Python

Use a formatted string literal to print the binary representation of a number, e.g. print(f'{number:b}').

Formatted string literals enable us to use the format specification mini-language which can be used to get the binary representation of a number.

main.py
number = 13 # โœ… format number as binary (in base 2) string = f'{number:b}' print(string) # ๐Ÿ‘‰๏ธ 1101 # โœ… Convert an integer to a binary string prefixed with 0b string = bin(number) print(string) # ๐Ÿ‘‰๏ธ 0b1101 # โœ… convert an integer to a lowercase hexadecimal string prefixed with 0x string = hex(number) print(string) # ๐Ÿ‘‰๏ธ 0xd

print binary representation of number

The code for this article is available on GitHub

The first example uses a formatted string literal.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
var1 = 'bobby' var2 = 'hadz' result = f'{var1}{var2}' print(result) # ๐Ÿ‘‰๏ธ bobbyhadz

Make sure to wrap expressions in curly braces - {expression}.

Formatted string literals also enable us to use the format specification mini-language in expression blocks.

main.py
number = 13 string = f'{number:b}' print(string) # ๐Ÿ‘‰๏ธ 1101

The b character stands for binary format and outputs the number in base 2.

You can prefix the b character with a digit to specify the width of the string.

main.py
number = 13 string = f'{number:8b}' print(repr(string)) # ๐Ÿ‘‰๏ธ ' 1101'

If you need to pad the number with zeros instead of spaces, add a zero after the colon.

main.py
number = 13 string = f'{number:08b}' print(repr(string)) # ๐Ÿ‘‰๏ธ '00001101'

You can also prefix the result with 0b if necessary.

main.py
number = 13 string = f'0b{number:08b}' print(repr(string)) # ๐Ÿ‘‰๏ธ '0b00001101'

# Print the binary representation of a Number using bin()

Alternatively, you can use the bin() function.

main.py
number = 13 string = bin(number) print(string) # ๐Ÿ‘‰๏ธ 0b1101 print(bin(3)) # ๐Ÿ‘‰๏ธ 0b11 print(bin(10)) # ๐Ÿ‘‰๏ธ 0b1010

print binary representation of number using bin

The code for this article is available on GitHub

The bin() function converts an integer to a binary string prefixed with 0b.

You can also use a formatted string literal to add or remove the 0b prefix.

main.py
number = 13 string = f'{number:#b}' print(string) # ๐Ÿ‘‰๏ธ 0b1101 string = f'{number:b}' print(string) # ๐Ÿ‘‰๏ธ 1101
When used with integers, the # option adds the respective prefix to the binary, octal or hexadecimal output, e.g. 0b, 0o, 0x.

If you need to get the hexadecimal representation of a number, use the hex() function.

main.py
number = 13 string = hex(number) print(string) # ๐Ÿ‘‰๏ธ 0xd

The hex() function converts an integer to a lowercase hexadecimal string prefixed with 0x.

# Convert the integer to an uppercase or lowercase hexadecimal string

You can also use a formatted string literal to convert an integer to an uppercase or lowercase hexadecimal string with or without the prefix.

main.py
number = 13 string = f'{number:#x}' print(string) # ๐Ÿ‘‰๏ธ 0xd string = f'{number:x}' print(string) # ๐Ÿ‘‰๏ธ d string = f'{number:X}' print(string) # ๐Ÿ‘‰๏ธ D

convert integer to uppercase or lowercase hexadecimal string

The code for this article is available on GitHub

The x character stands for hex format. It outputs the number in base 16 using lowercase letters for the digits above 9.

The X character does the same but uses uppercase letters for the digits above 9.

# Convert an integer to binary and keep leading zeros - Python

Use the format() function to convert an integer to binary and keep the leading zeros.

main.py
integer = 27 # โœ… convert integer to binary keeping leading zeros (with 0b prefix) result = format(integer, '#08b') print(result) # ๐Ÿ‘‰๏ธ 0b011011 # โœ… without 0b prefix (length 6) result = format(integer, '06b') print(result) # ๐Ÿ‘‰๏ธ 011011

convert integer to uppercase or lowercase hexadecimal string

The code for this article is available on GitHub
If you'd rather use a formatted string literal, scroll down to the next subheading.

We used the format() function to convert an integer to binary and keep the leading zeros.

The function converts the provided value to a formatted representation according to the format_spec argument.

The syntax for the format_spec argument is determined by the format specification mini-language.

When used with integers, the # option adds the respective prefix to the binary, octal or hexadecimal output, e.g. 0b, 0o, 0x.
main.py
integer = 27 result = format(integer, '#08b') print(result) # ๐Ÿ‘‰๏ธ 0b011011

If you don't need the 0b prefix, remove the # option.

main.py
integer = 27 result = format(integer, '06b') print(result) # ๐Ÿ‘‰๏ธ 011011

The 0 digit is the fill character and the digit after the 0 is the width of the string.

The b character stands for binary format and outputs the number in base 2.

You can prefix the b character with any digit to adjust the width of the string.

main.py
integer = 27 result = format(integer, '#010b') print(result) # ๐Ÿ‘‰๏ธ 0b00011011 result = format(integer, '08b') print(result) # ๐Ÿ‘‰๏ธ 00011011

# Convert an integer to binary and keep leading zeros using f-string

Alternatively, you can use a formatted string literal.

main.py
integer = 27 # โœ… with 0b prefix (length 8) result = f'{integer:#08b}' print(result) # ๐Ÿ‘‰๏ธ 0b011011 # โœ… without 0b prefix (length 6) result = f'{integer:06b}' print(result) # ๐Ÿ‘‰๏ธ 011011

convert integer to binary and keep leading zeros using f string

The code for this article is available on GitHub
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
var1 = 'bobby' var2 = 'hadz' result = f'{var1}{var2}' print(result) # ๐Ÿ‘‰๏ธ bobbyhadz

Make sure to wrap expressions in curly braces - {expression}.

Formatted string literals also enable us to use the format specification mini-language in expression blocks.

main.py
integer = 27 result = f'{integer:#08b}' print(result) # ๐Ÿ‘‰๏ธ 0b011011

Just like with the format function, the b character stands for binary format.

If you need to remove the 0b prefix, remove the # symbol.

main.py
integer = 27 result = f'{integer:06b}' print(result) # ๐Ÿ‘‰๏ธ 011011

If you need to change the width of the string, adjust the digit before the b character.

main.py
integer = 27 result = f'{integer:010b}' print(result) # ๐Ÿ‘‰๏ธ 0000011011

The number is formatted in base 2, left-filled with zeros to the specified width.

# Convert an integer to binary and keep leading zeros using str.format()

Alternatively, you can use the str.format() method.

main.py
integer = 27 result = '{:#08b}'.format(integer) print(result) # ๐Ÿ‘‰๏ธ 0b011011 result = '{:06b}'.format(integer) print(result) # ๐Ÿ‘‰๏ธ 011011
The code for this article is available on GitHub

The str.format() method performs string formatting operations.

main.py
first = 'Bobby' last = 'Hadz' result = "His name is {} {}".format(first, last) print(result) # ๐Ÿ‘‰๏ธ "His name is Bobby Hadz"

The string the method is called on can contain replacement fields specified using curly braces {}.

The str.format() method also makes use of the format specification mini-language, so we used the same syntax as in the previous examples.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev