16

I am trying to convert an integer to a binary number, but 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.

0

11 Answers 11

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'
Sign up to request clarification or add additional context in comments.

Comments

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) 

Comments

5

It's easy just make this function:

def f(n):print('{:0b}'.format(n))
f(17)
>>> 10001

Comments

4
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

1 Comment

hexadecimal is 'x', not 'h' -- for all specifiers, see docs.python.org/3/library/…
3

with Python 3.6 you can use f-strings

print( f'{x:b}' )
'10001'

Comments

2
bin(n).replace("0b", "") 

This one is using replace Where n is the provided decimal

Comments

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

1 Comment

beware, for 0 this returns an empty string.
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

Comments

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'}'

1 Comment

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.
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'

Comments

-1
print (bin(int(input().strip()))[2:])

Pythonic way to solve. ;)

1 Comment

In what sense is this "pythonic"?

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.