- 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: Int to Binary (Convert Integer to Binary String)
- What are Binary Strings for Integers?
- Use Python bin to Convert Int to Binary
- Use Python String Formatting to Convert Int to Binary
- Use Python f-strings to Convert Int to Binary
- Use Python format to Convert Int to Binary
- Convert an Int to Binary in Python without a Function
- Conclusion
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: Int to Binary (Convert Integer to Binary String)
You’ll learn a brief overview of this conversion and how binary strings are represented in computers. Then, you’ll learn how to use four different methods to use Python to convert int to binary. These include, the bin() function, string formatting, f-strings, the format() function, as well as naive implementation without the use of any functions.
The Quick Answer: Use the format() Function
What are Binary Strings for Integers?
The common integer system that we’re used to, the decimal system, uses a base of ten, meaning that it has ten different symbols. These symbols are the numbers from 0 through to 9, which allow us to make all combinations of numbers that we’re familiar with.
Binary strings, on the other hand, use a base of two, meaning that they only have two numbers to express different numbers. These numbers are either 0 or 1. While the binary number system has been in use in different ancient civilizations (such as Egypt and India), it is used extensively in electronics and computer system in modern times.
In the next sections, you’ll learn how to use Python to convert an integer to a binary using the bin() function.
Want to learn how to get a file’s extension in Python? This tutorial will teach you how to use the os and pathlib libraries to do just that!
Use Python bin to Convert Int to Binary
The Python bin() function is short for binary and allows us to convert an integer to a binary string, which is prefixed by ‘0b’ . In later section, you’ll learn how to convert the integer using Python without the prefix.
Let’s take a look at how we can turn a positive integer into a binary string using Python:
# Convert an integer to a binary string using Python bin() positive = 123 binary = bin(positive) print(binary) Returns: '0b1111011'
We can see that a binary string with the ‘0b’ prefix has been returned.
Let’s check the type of the statement that’s been returned, using the built-in type() function:
# Checking the type of our binary string positive = 123 binary = bin(positive) print(type(binary)) # Returns: #
We can see here that the function returned a string, as expected.
Now let’s see what happens when we pass in a negative integer and try to use Python to convert it to binary string:
# Convert an integer to a binary string using Python bin() negative = -123 binary = bin(negative) print(binary) Returns: '-0b1111011'
We can see that there’s also a ‘-‘ prefix to our string, letting us know that the number is a negative value.
In the next section, you’ll learn how to use Python string formatting to convert an int to a binary string.
Want to learn how to use the Python zip() function to iterate over two lists? This tutorial teaches you exactly what the zip() function does and shows you some creative ways to use the function.
Use Python String Formatting to Convert Int to Binary
If you’re wanting to convert a Python integer to a binary string without the ‘0b’ prefix, you can use string formatting.
Python string formatting allows us to define different format types for passing in values. In this case, we’ll pass in the format code of » , which allows us to convert an integer to binary.
Let’s see how we can pass in a few integer values, both positive and negative, and convert them to binary string using string formatting:
# Convert an integer to a binary string using Python string formatting positive = 123 negative = -123 positive_binary = ''.format(positive) negative_binary = ''.format(negative) print(f'') print(f'') # Returns: # positive_binary='1111011' # negative_binary='-1111011'
We can see here that this method returns the same strings, without the ‘0b’ prefix.
In the next section, you’ll learn Python f-strings to convert an int to a binary string.
Want to learn more about Python for-loops? Check out my in-depth tutorial that takes your from beginner to advanced for-loops user! Want to watch a video instead? Check out my YouTube tutorial here.
Use Python f-strings to Convert Int to Binary
Python f-strings allow us to make string formatting a little bit more intuitive. They also allow us to apply formatting to our strings in similar ways to traditional string formatting.
As a quick refresher on Python f-strings, they’re strings introduced in Python versions 3.6 and above, and are created by prefixing either a ‘f’ or ‘F’ to the string.
Let’s see how we can convert an integer to a binary string using Python f-strings. We’ll try this for the same positive and negative integers as above:
# Convert an integer to a binary string using Python f-strings positive = 123 negative = -123 positive_binary = f'' negative_binary = f'' print(f'') print(f'') # Returns: # positive_binary='1111011' # negative_binary='-1111011'
We can see here that the same values are returned. Python f-strings may not work in all versions of Python, but they are intuitive and easy to understand.
In the next section, you’ll learn how to use the Python format() function to convert an int to a binary string.
Want to learn more about Python f-strings? Check out my in-depth tutorial, which includes a step-by-step video to master Python f-strings!
Use Python format to Convert Int to Binary
Another way to convert a Python integer to a binary string is to use the built-in format() function. The format() function takes value and a format spec as its arguments.
Because of this, we can pass in a value (in this case, an integer) and a format spec (in this case “b”), to specify that we want to return a binary string.
Let’s see how we can accomplish this using Python:
# Convert an integer to a binary string using Python format() positive = 123 negative = -123 positive_binary = format(positive, 'b') negative_binary = format(negative, 'b') print(positive_binary) print(negative_binary) # Returns: # positive_binary='1111011' # negative_binary='-1111011'
This is also a very readable way in which we can convert Python integers to string. The function makes it clear that we’re converting a value to something else, even specifying a type.
In the final section, you’ll learn how to convert an int to a binary string from scratch.
Want to learn how to calculate and use the natural logarithm in Python. Check out my tutorial here, which will teach you everything you need to know about how to calculate it in Python.
Convert an Int to Binary in Python without a Function
In this final section, you’ll learn how to convert how to create a naive method to convert a Python integer to a string. You’ll actually create a custom function that does this, but be able to understand how the conversion works.
Practically speaking, this is not something you’d really need to do, but it can be a helpful task to understand for programming interviews.
# Convert an integer to a binary string using a custom function def int_to_binary(integer): binary_string = '' while(integer > 0): digit = integer % 2 binary_string += str(digit) integer = integer // 2 binary_string = binary_string[::-1] return binary_string print(int_to_binary(123)) # Returns: # 1111011
Conclusion
In this post, you learned how to use Python to convert int to binary, meaning how to convert integer values to binary strings. You learned how to do this using a number of different methods, including using the Python bin() function, string formatting, f-strings, the format() function, and without any functions at all.
If you want to learn more about the Python bin() function, check out the official documentation here. To learn more about the Python format() function, you can find the official documentation here.