In this post, we will explore How to do Convert int to binary in Python using the built-in method and str.format() and f-string.
1. bin() function to Convert int to binary in python
The Python inbuilt bin() function is used to get an integer’s value binary representation that it returns as a string prefixed with “0b”.The value should be a valid Python int object. To remove the prefixed “0b” can use slicing.
Syntax
bin(val)
Parameter
- Val: The number that we want to convert into binary string
Python Program to convert int to binary
mynum = 45
print(bin(mynum))
#eliminate "ob" prefix
mynum = 45
bin_val = bin(mynum)[2:]
print(bin_val)
Output
0b101101
101101
Python Program to convert int to 16-bit binary
In this example, we are using the bin() function to convert int number to 16-bit binary.
num = 45
binary= bin(num)[2:].zfill(16)
print(binary)
Output
0000000000101101
2. str.format() to convert int to binary in python
The str. format() method can be used to convert an integer number to binary using string presentation “b”. Let us understand with an example.
Python Program to convert integer to binary
num = 45
binary = "{0:b}".format(num)
print(binary)
Output
101101
Python int to 16-bit binary Using format()
In this example, we are using the format() function to convert int number to 16-bit binary.
num = 45
binary= '{0:016b}'.format(num)
print(binary)
Output
0000000000101101
3. Using format() to Convert int to binary in python
The easy way to remove the “0b” prefix is the Python built-in format() function. It takes two arguments the first argument is the value we want to convert into binary and the Second argument is a format that covert value to the given format.
Syntax
format(val,format)
Parameters
- val: The number that we want to convert into binary string
- Format : It convert the passed value to given format.
Format | Description |
= | Places the sign to the leftmost position |
b | Convert value to Binary format. |
c | Converts the value to Unicode character |
d | Convert value to Binary to Decimal format |
o | Convert the value to Octal format |
x | Convert the value to Hex format in lower case. |
E | Convert value to scientific format with upper case E |
< | Left align the output |
> | Right align the output |
_ | Use to add thousand separator |
Python Program to int to binary Using format()
In this example, we convert int to binary bypassing b as a format. more formates
binary = format(45, "b")
print(binary)
Output
101101
4. f-string to convert int to binary in python
Python version 3.6 introduced the formatted string literal also known as “f-string”. To use it we have to start the string with “F” or “f” before quotation marks and write a Python expression or variable that contains literal values between { } that are evaluated at runtime. The f-string controls the final output of the formatted string.
Python Program to convert int to binary with leading zero
num = 45
binary = f"{num:08b}"
print(binary)
Output
00101101
Python Program to convert int to binary Without leading zero
num = 45
binary = f"{num:b}"
print(binary)
Output
101101
Summary
In this post, we have learned how to do Convert int to binary in python by using the built-in function bin() and format() str.format() and f-string.