# Python Integer Binary Conversion & How To Make Python bin() Function Without 0b Prefix

In this article, I will tell you how to use the python `bin()` function to convert an integer to it’s binary value and how to remove the the prefix ‘0b’ from the binary result value. It will also tell you how to convert a binary value to the integer value.

## 1. How To Convert Integer To Binary In Python.

1. The `bin()` function in Python is used to convert an integer to its binary representation.
2. The function takes a single argument, which must be an integer, and returns a string representing the binary value of the integer.
3. Here’s an example:
```>>> bin(10)
'0b1010'
>>>```
4. In the above example, we pass the integer value `10` to the `bin()` function, which returns the binary representation of `10` as the string `'0b1010'`.
5. The `'0b'` prefix indicates that this string represents a binary value.
6. We can also use the `bin()` function with negative integers:
```>>> bin(-10)
'-0b1010'```
7. In this example, we pass the integer value `-10` to the `bin()` function, which returns the binary representation of `-10` as the string `'-0b1010'`. The `'-'` sign indicates that this value is negative.

## 2. How To Make Python bin() Function Without 0b Prefix.

1. We can also use the `format()` function to format the binary string without the `'0b'` prefix:
```>>> format(10, 'b')
'1010'```
2. In this example, we pass the integer value `10` and the format specifier `'b'` to the `format()` function, which returns the binary representation of `10` as the string `'1010'`.

## 3. How To Convert Binary To Integer In Python.

1. In Python, you can use the `int` function to convert a binary string to an integer.
2. The second argument of the `int` function specifies the base of the string to be converted.
3. For binary strings, the base should be 2.
4. Here is an example code:
```>>> binary_string = "111010"
>>>
>>> decimal_number = int(binary_string, 2)
>>>
>>> print(decimal_number)
58```
5. This will output the integer 58, because the binary string 101010 converts to the decimal number 58.

This site uses Akismet to reduce spam. Learn how your comment data is processed.