- How to Convert Int to Binary in Python
- Method 1: Using bin() Function
- Method 2: Using format() Function
- Method 3: Using str.format() Method
- Method 4: Using the f-string Function
- Conclusion
- Python print number as binary
- # Table of Contents
- # Print the binary representation of a Number in Python
- # Print the binary representation of a Number using bin()
- # Convert the integer to an uppercase or lowercase hexadecimal string
- # Convert an integer to binary and keep leading zeros — Python
How to Convert Int to Binary in Python
Any number system represented in the form of “1,0” is referred to as a Binary number. Binary numbers are used in machine language and many modern electronic devices. Other number systems are converted into Binary numbers to make them machine understandable/readable. Among them, the “Int” is used the most, and the need to convert “int” to “binary” frequently arises in Python programs.
In this article, we are going to discuss different methods that are used to convert integers into binary in Python:
Method 1: Using bin() Function
The “bin()” is a built-in function of Python used to convert the “integer” into “binary” and represent the output in string format. Binary is represented by the prefix “0b” in string format.
Let’s understand the working of the “bin()” function with the below-given code.
#conversion into binary using bin() Number_1 = 10 Number_2 = 77 print('conversion of Int into Binary is :', bin(Number_1)) print('conversion of Int into Binary is:', bin(Number_2))
- Two integer values named ”Number_1” and “Number_2” are initialized.
- The “bin() function” is used to convert the integer number into a binary number by taking the value of a variable into its parameter.
The integer numbers are converted into binary numbers in the above output.
Method 2: Using format() Function
In Python, the “format()” function is also used to convert the Integer value into a binary value. The converted value is represented in string format as the “format()” function converts the value according to user formatting specifications. The “format()” function allows us to convert integers into binary using the “b” (binary) format code.
Let’s understand how the “format()” function is used to converts the int to binary:
#conversion into binary using format() Number_1 = 7 Number_2 = 16 Number_3 = format(Number_1, "b") Number_4 = format(Number_2, "b") print('conversion of number_1 into binary is: ',Number_3) print('conversion of number_2 into binary is: ',Number_4)
- Two integer values named ”Number_1” and “Number_2” are initialized.
- The “format() function” is used to convert the integer number into a binary number by taking the value of a variable into one parameter and the format value “b” into its second parameter.
The above output displays the newly converted numbers to binary.
Method 3: Using str.format() Method
The “str.format()” method is just like the “format()” function. It takes input variables into its parameter and the format code inside the curly braces. To convert integers into binary, we defined the “ ” format code before the “.format” method inside the quotation marks of the string.
Let’s understand the “str.format()” method by the following example:
#conversion of int into binary using str.format() int_1 = 15 binary = "".format(int_1) print('conversion of int into binary is: ',binary)
- The integer value named ”Int_1” is initialized.
- The “str.format()” function converts the “int_1” into a binary.
- The variable “int_1” is passed in the format() function parameter and inside the curly braces “<>”. The type “b” is used to specify the binary conversion, and “0” is used to place the variable at argument position 0.
The converted number is printed on the console.
Method 4: Using the f-string Function
Another built-in function named “f-string” is used to convert the integer value into the binary value. To convert integers to binary using “f-string”, we input the format code “b” inside the curly braces of the “f-string”.
Let’s understand the concept of the “f-string” by the following example:
# Convert into binary using f-strings int_1 = 522 int_2 = -23 binary_1 = f'' binary_2 = f'' print('Binary number of intger 1 is: ',binary_1) print('Binary number of intger 2 is: ',binary_2)
- Two variables named “int_1” and “int_2” are initialized.
- “f-string” method converts the number into binary by passing the “b” inside the curly braces with variable values.
- “f-string” formatting is used for string conversion mainly so the new binary value will be in string type.
In the above output, the positive and negative integer values have been converted into their respective binary numbers.
That’s all from this Python guide!
Conclusion
In Python, the “bin()” function, “format()” function, “f-string()”function, and “str.format()” method can convert Integer value into a binary value. The “bin()” function is the simplest among all others as it takes a variable value into its parameter and returns the output in binary. The “format()” and “str.format()” functions are very similar to each other; they can convert the value into binary by passing a format code. This post presented a deep overview of all the methods used to convert integer values into strings.
Python print number as binary
Last updated: Jan 28, 2023
Reading time · 4 min
# Table of Contents
# 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») .
Formatted string literals enable us to use the format specification mini-language which can be used to get the binary representation of a number.
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
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f .
Copied!var1 = 'bobby' var2 = 'hadz' result = f'var1>var2>' print(result) # 👉️ bobbyhadz
Make sure to wrap expressions in curly braces — .
Formatted string literals also enable us to use the format specification mini-language in expression blocks.
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.
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.
Copied!number = 13 string = f'number:08b>' print(repr(string)) # 👉️ '00001101'
You can also prefix the result with 0b if necessary.
Copied!number = 13 string = f'0bnumber:08b>' print(repr(string)) # 👉️ '0b00001101'
# Print the binary representation of a Number using bin()
Alternatively, you can use the bin() function.
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.
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.
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.
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.
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