Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Convert an Integer to Binary String in Python

  • 05 Sep, 2025
  • Com 3
How to Convert an Integer to Binary String in Python

The bin() function is the most efficient way to convert an integer to a binary string in Python. It accepts an integer and returns its equivalent binary string, prefixed with ‘0b’.

It works with both positive and negative integers.

Converting an integer to binary using bin() function

In the above figure, we described both types of integers (positive and negative) and converted them into a binary string. Both outputs are different.

positive_num = 22

negative_num = -22

binary_str = bin(positive_num)

print(binary_str)

# Output: 0b10110

binary_str = bin(negative_num)

print(binary_str)

# Output: -0b10110

As you can see, both outputs are now binary strings with “0b” prefix.

You can remove the prefix “0b” by slicing the string for the clean output.

positive_num = 22

binary_str = bin(positive_num)

binary_str_clean = binary_str[2:]

print(binary_str_clean)

# Output: 10110

You can see that the output binary string is very clean, which is quick for debugging.

Other approaches

Approach 1: Using f-strings (Python 3.6+)

With the release of Python 3.6, it comes with a new approach called “f-strings”, which is a clean method to embed formatting directly in strings for readability.

By using the format specifier :b within an f-string, an integer can be seamlessly converted into its binary string representation.

Using f-strings (Python 3.6+)

positive_num = 22

negative_num = -22

print(f'{positive_num:b}')

# Output: 10110

print(f'{negative_num:b}')

# Output: -10110

You can see that we don’t need slicing or anything to remove the ‘0b’ prefix because it is not added to the string. It might be the cleanest approach for the conversion.

Approach 2: Using format()

The format() function offers flexibility, including the ability to specify the bit length. This function accepts two parameters: The value to be formatted and the format specifier. It returns the output formatted according to the specified format.

Using format() method in Python

 

positive_num = 22

negative_num = -22

binary_str = format(positive_num, "b")

print(binary_str)

# Output: 10110

binary_str = format(negative_num, "b")

print(binary_str)

# Output: -10110

Specify bit length (e.g., 8 bits)

positive_num = 22

negative_num = -22

binary_str_octal = format(positive_num, '08b')

print(binary_str_octal)

# Output: 00010110

binary_str_neg_octal = binary_str = format(negative_num, '08b')

print(binary_str_neg_octal)

# Output: -0010110

The output is an 8-bit binary string.

Approach 3: Using string.format()

The string.format() method formats the specified values and inserts them inside the string’s placeholders, which are defined using curly brackets: {}.

The ‘b’ specifier indicates that the number should be formatted as a binary number.

Using string.format() method

positive_num = 22

negative_num = -22

print("{0:b}".format(positive_num))

# Output: 10110

print("{0:b}".format(negative_num))

# Output: -10110

That’s all!

Post Views: 14
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Get First and Last Elements of Tuple in Python
Converting a NumPy Array to Python List

3 Comments

  1. SY

    May 26, 2022 at 7:17 am

    Kindof an overkill to use format() to remove the binary string prefix, when you could just use string slicing ‘[2:]’ which is much simpler.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend