15

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.

albert
  • 8,285
  • 3
  • 19
  • 32
VChocolate
  • 191
  • 1
  • 1
  • 7

11 Answers11

20

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'
Vedang Mehta
  • 2,214
  • 9
  • 22
5

use python string slice operation.

a = bin(17)
b = bin(17)[2:]

to format this to 8-bits use zfill.

c = b.zfill(8) 
Tanu
  • 1,503
  • 12
  • 21
5

It's easy just make this function:

def f(n):print('{:0b}'.format(n))
f(17)
>>> 10001
Draken
  • 3,134
  • 13
  • 34
  • 54
just 4 help
  • 51
  • 1
  • 1
3
format(17, 'b')
>>> '10001'

Use the format() builtin. It also works for hexadecimal, simply replace 'b' with 'x'.

https://docs.python.org/3/library/functions.html#format

NuclearPeon
  • 5,743
  • 4
  • 44
  • 52
  • 1
    hexadecimal is 'x', not 'h' -- for all specifiers, see https://docs.python.org/3/library/string.html#format-specification-mini-language – NikT Jun 29 '21 at 16:26
2
bin(n).replace("0b", "") 

This one is using replace Where n is the provided decimal

2

with Python 3.6 you can use f-strings

print( f'{x:b}' )
'10001'
Diego Roccia
  • 179
  • 5
1

I do not know why nobody suggested using lstrip.

integer = 17
bit_string = bin(integer)
final = bit_string.lstrip('-0b') # minus to also handle negations
print(final) # prints 10001
mbpaulus
  • 7,301
  • 3
  • 29
  • 40
0

inhexa=(hexanum.get()) # gets the hexa value dec = int(inhexa,16) #changes the base ensures conversion into base 16 to interger

 b=bin(dec)[2:]  #converts int/dec into binary and shows string except first two digits
ashiq pervez
  • 45
  • 1
  • 2
  • 15
0

Since this page will answer to developers performing byte handling therefore performance oriented there should be a benchmarked comparison of above methods.

Assuming we do not require padding (a subject this thread tackles) the aforementioned solutions (including the top answer from the other thread) yield these results (for 10 million random 21-bit integers) : Results

Benchmark can be find here.

So the f'{x:'b'}' proves faster with the intuitive slicing method as a close second (results were consistent between runs on a r5-3600 cpu and 16GB of 2133MHz 19CL system memory).

In the end the answer from Diego Roccia was the fastest and pretty elegant.

Padding with leading zeros and further options of said method can be found here but using the f'{x:'b'}' with .zfill() for padding is faster than the solutions given there (find actual tests in here).

So the answer is: f'{x:'b'}'

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 12 '23 at 20:41
0

Well, the easiest way, for Python 3.9.0 and above is by using String removeprefix() function which removes the prefix and returns the rest of the string.

x = 17

y = bin(17)
>>>'0b10001'

y.removeprefix("0b")
>>> '10001'
Dave K
  • 98
  • 1
  • 10
-1
print (bin(int(input().strip()))[2:])

Pythonic way to solve. ;)

Ayan B.
  • 199
  • 1
  • 1
  • 10