How to Convert Integers to Binary Strings in Python
This guide explains how to convert integers to their binary string representations in Python. We'll use the built-in bin()
function and f-strings (formatted string literals) to achieve this, covering options for including or omitting the 0b
prefix and padding with leading zeros. We'll also touch on hexadecimal representation.
Using bin()
(with "0b" prefix)
The built-in bin()
function is the simplest way to get the binary representation of an integer as a string. It includes the prefix "0b":
number = 13
string = bin(number)
print(string) # Output: 0b1101
print(bin(3)) # Output: 0b11
print(bin(10)) # Output: 0b1010
bin(number)
: Converts the integernumber
to its binary string representation, prefixed with "0b".
Using f-strings (Without "0b" Prefix, Padding)
f-strings (formatted string literals) provide more control over the output format. You can easily omit the 0b
prefix and add leading zeros for padding.
number = 13
# Binary without prefix
string = f'{number:b}' # 'b' for binary
print(string) # Output: 1101
# Binary with leading zeros (padded to 8 bits)
string = f'{number:08b}' # '0' for zero-padding, '8' for width, 'b' for binary
print(string) # Output: 00001101
# With "0b" prefix and padding (using #)
string = f'{number:#010b}' # '0b' prefix with padding.
print(string) # Output: 0b00001101
-
The
f'{number:b}'
formats the number to binary. -
The b character can be used for binary representation.
-
If a zero is added before the number, and no alignment option is specified, then the number will be padded with zeros.
-
f'{number:b}'
: Binary representation without the0b
prefix. -
f'{number:08b}'
: Binary, padded with leading zeros to a width of 8 characters. -
f'{number:#010b}'
: Binary, with0b
prefix and padded with leading zeros to a total width of 10 characters (including the0b
). The#
option adds the0b
prefix.
You can also specify a variable to be the width of the number:
number = 13
width_of_string = 8
string = f'{number:0{width_of_string}b}' # Add a variable to the format
print(string) # Output: 00001101
Hexadecimal Representation
While not the main focus, here's how to get hexadecimal output (base-16), as it's closely related:
number = 13
hex_string = hex(number) # With "0x" prefix
print(hex_string) # Output: 0xd
hex_string_lower = f'{number:x}' # Lowercase hex, no prefix
print(hex_string_lower) # Output: d
hex_string_upper = f'{number:X}' # Uppercase hex, no prefix
print(hex_string_upper) # Output: D
hex_padded = f'{number:04X}' # Uppercase, padded to 4 digits
print(hex_padded) # Output: 000D
hex(number)
: Returns the hexadecimal string with a "0x" prefix.f'{number:x}'
: Lowercase hexadecimal, no prefix.f'{number:X}'
: Uppercase hexadecimal, no prefix.f'{number:04X}'
: Uppercase, padded with leading zeros to 4 digits.
Conclusion
This guide demonstrated multiple ways to convert a number to its binary representation, using bin()
and f-strings, as well as showed the hex()
function that converts the number to its hexadecimal representation.
- The
bin()
function is the simplest way to convert an integer to its binary representation. - For greater control over formatting (removing the
0b
prefix, padding with zeros), f-strings are the most flexible and readable option.
Remember that these methods convert integers to strings; the underlying numerical value remains unchanged.