Search code examples
pythonstringcharacter

Remove the 0b in binary


I am trying to convert a binary number I have to take out the 0b string out.

I understand how to get a bin number

x = 17

print(bin(17))

'0b10001'

but I want to take the 0b in the string out and I am having some issues with doing this. This is going to be within a function returning a binary number without the 0b.


Solution

  • Use slice operation to remove the first two characters.

    In [1]: x = 17
    
    In [2]: y = bin(x)[2:]
    
    In [3]: y
    Out[3]: '10001'