# Print the binary representation of a Number in Python

Last updated: Apr 10, 2024
4 min

## #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
```Copied!```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.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with `f`.
main.py
```Copied!```var1 = 'bobby'

result = f'{var1}{var2}'
``````

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
```Copied!```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
```Copied!```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
```Copied!```number = 13

string = f'{number:08b}'
print(repr(string))  # ๐๏ธ '00001101'
``````

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

main.py
```Copied!```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
```Copied!```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.

main.py
```Copied!```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
```Copied!```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
```Copied!```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.

## #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
```Copied!```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
``````

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
```Copied!```integer = 27

result = format(integer, '#08b')
print(result)  # ๐๏ธ 0b011011
``````

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

main.py
```Copied!```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
```Copied!```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
```Copied!```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
``````

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with `f`.
main.py
```Copied!```var1 = 'bobby'

result = f'{var1}{var2}'
``````

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
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```integer = 27

result = '{:#08b}'.format(integer)
print(result)  # ๐๏ธ 0b011011

result = '{:06b}'.format(integer)
print(result)  # ๐๏ธ 011011
``````

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

main.py
```Copied!```first = 'Bobby'

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.