# Encoding System that Assign Same Number of Bits for Each Character

I am trying to get a binary string that has been converted from text of a text file, I am able to get that but the problem is, I need each character to be represented by same number of bits, but that is not what I get (please see the below python code and corresponding output). For example, character i is represented by 1101001, which is 7 bits long, but character ! is represented by 100001, which is 6 bits long.

Is there any encoding/decoding system where each character takes same amount of bits?

content = open('a.txt', 'r').read()
test_str = content
# using join() + ord() + format()  ... Converting String to binary

Binary = ' '.join(format(ord(i), 'b') for i in test_str)

#Decimal=int(Binary, 2)

# printing original string
print("The original string is : " + str(test_str))
# printing result
print("The string after Binary conversion : \n" + str(Binary))


Output:

The original string is : Hi! Is there a solution?
The string after Binary conversion :
1001000 1101001 100001 100000 1001001 1110011 100000 1110100 1101000 1100101 1110010 1100101 100000 1100001 100000 1110011 1101111 1101100 1110101 1110100 1101001 1101111 1101110 111111

• This is a python-specific programming question. The C counterpart supports obligatory leading zeroes (e.g. %07b in systems that support %b), and the python format likely also has such a mechanism. If not, it is easy to implement it yourself. You're a programmer! Be creative. – Yuval Filmus Mar 15 '20 at 22:30

The usual way to solve this problem is by adding leading zeroes. So i would still be represented by 1101001, while ! will be represented by 0100001.