If you print a binary number, Python uses the prefix
'0b' to indicate that it’s a number in the binary system and not in the decimal system like normal integers.
print(bin(42)) # 0b101010
However, if you already know that the output numbers are binary, you don’t necessarily need the
How to print binary numbers without the
Method 1: Slicing
To skip the prefix, use slicing and start with index 2 on the binary string. For example, to skip the prefix
'0b' on the result of
x=bin(42)='0b101010', use the slicing operation
x[2:] that results in just the binary number
'101010' without the prefix
x = bin(42) print(x) # 0b101010 print(x[2:]) # 101010
Feel free to dive into the
bin() built-in function in this video tutorial:
But what if you actually want to replace the prefix
'0b' with the prefix
'00' so that the resulting string has the same length?
Method 2: Slicing + zfill()
string.zfill() method fills the string from the left with
'0' characters. In combination with slicing from the third character, you can easily construct a binary string without leading
'0b' characters and with leading
'0' characters up to the length passed into the
print(bin(42)[2:].zfill(8)) # 00101010
Alternatively, if you want to create a string with 12 characters, use
print(bin(42)[2:].zfill(12)) # 000000101010
You can learn more about
zfill() in this video about Python string methods:
Method 3: Negative Binaries
If you need to handle negative binaries, the above methods do not work because the binary number now needs to replace the second and third character
'0b'. For example, the binary number
'-0b101010'. You cannot simply skip the first two characters to obtain the correct result, can you? At the same time, if you always skipped or replaced the second and third characters, it wouldn’t work for positive numbers either. So what to do?
For a positive or negative binary to print it without the
'0b' prefix or
'-0b' prefix, you can simply use the
string.replace() method and replace each occurrence of
'0'. The resulting string is mathematically correct because leading
'0's don’t change the value of the number.
# Negative Binary print(bin(-42).replace('b', '0')) # -00101010 # Positive Binary print(bin(42).replace('b', '0')) # 00101010
You can learn more about the replacing functionality here:
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.