Last updated: Apr 10, 2024
Reading timeยท4 min
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.
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
The first example uses a formatted string literal.
f
.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.
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.
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.
number = 13 string = f'{number:08b}' print(repr(string)) # ๐๏ธ '00001101'
You can also prefix the result with 0b
if necessary.
number = 13 string = f'0b{number:08b}' print(repr(string)) # ๐๏ธ '0b00001101'
Alternatively, you can use the bin()
function.
number = 13 string = bin(number) print(string) # ๐๏ธ 0b1101 print(bin(3)) # ๐๏ธ 0b11 print(bin(10)) # ๐๏ธ 0b1010
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.
number = 13 string = f'{number:#b}' print(string) # ๐๏ธ 0b1101 string = f'{number:b}' print(string) # ๐๏ธ 1101
#
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.
number = 13 string = hex(number) print(string) # ๐๏ธ 0xd
The hex() function
converts an integer to a lowercase hexadecimal string prefixed with 0x
.
You can also use a formatted string literal to convert an integer to an uppercase or lowercase hexadecimal string with or without the prefix.
number = 13 string = f'{number:#x}' print(string) # ๐๏ธ 0xd string = f'{number:x}' print(string) # ๐๏ธ d string = f'{number:X}' print(string) # ๐๏ธ D
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.
Use the format()
function to convert an integer to binary and keep the leading
zeros.
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
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.
#
option adds the respective prefix to the binary, octal or hexadecimal output, e.g. 0b
, 0o
, 0x
.integer = 27 result = format(integer, '#08b') print(result) # ๐๏ธ 0b011011
If you don't need the 0b
prefix, remove the #
option.
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.
integer = 27 result = format(integer, '#010b') print(result) # ๐๏ธ 0b00011011 result = format(integer, '08b') print(result) # ๐๏ธ 00011011
Alternatively, you can use a formatted string literal.
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
f
.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.
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.
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.
integer = 27 result = f'{integer:010b}' print(result) # ๐๏ธ 0000011011
The number is formatted in base 2, left-filled with zeros to the specified width.
Alternatively, you can use the str.format()
method.
integer = 27 result = '{:#08b}'.format(integer) print(result) # ๐๏ธ 0b011011 result = '{:06b}'.format(integer) print(result) # ๐๏ธ 011011
The str.format() method performs string formatting operations.
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.
You can learn more about the related topics by checking out the following tutorials: