3
x = '0b001'

I need x to be 0b001 so that is a binary number, I tried doing int('0b001') however the 'b' gives an error due to being a character.

How to convert '0b001' into a binary?

Shaido
  • 27,497
  • 23
  • 70
  • 73

2 Answers2

3

If you want to get a decimal value of binary string, you can use int() with base argument set to 2.

x = '0b001'
decimal_x = int(x, 2)
decimal_x = 1

If you want to remove the '0b' from the binary string, do a string slice as below

x = '0b001'
x = x[2:]
x = '001'
Ram
  • 4,724
  • 2
  • 14
  • 22
1

Try specifying the base number to 0:

>>> int('0b001', 0)
1
>>> 
U13-Forward
  • 69,221
  • 14
  • 89
  • 114