0

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))
1

2 Answers 2

1
string = "01101"
print(bin(int(string,2)))
Sign up to request clarification or add additional context in comments.

Comments

1

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'

1 Comment

Didn't realize you could specify a base within the int function, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.