Tom Lightfoot
Tom Lightfoot

Reputation: 23

String of bits to binary format Python

This seems really simple but I can't figure it out. I have a string of bits in a string format and want to convert it to a binary format. I assumed placing the string inside of the bin() function would work but it doesn't.

string = "01101"

print(bin(string))

Upvotes: 0

Views: 356

Answers (2)

Wolph
Wolph

Reputation: 80111

It depends what you mean by binary format.

Here's a few examples of what you can do:

>>> int('01101', 2)
13

>>> number = 13

>>> bin(number)
'0b1101'

>>> oct(number)
'0o15'

>>> hex(number)
'0xd'

>>> f'{number:08b}'
'00001101'

Upvotes: 1

Mas Zero
Mas Zero

Reputation: 593

string = "01101"
print(bin(int(string,2)))

Upvotes: 1

Related Questions