How to remove beginning of a string

So at school, we are meant to be making a binary and decimal converter. So far I have discovered the bin() function and it gives me roughly what I need:

bin(10)
#0b1010  

However, I was wandering how I would go about removing the 0b at the beginning so that I am left with simply 1010.

Does anyone actually know what the 0b means?

Upvotes: 0

Views: 310

Answers (3)

kyle k
kyle k

Reputation: 5512

There is another way to convert a number to binary without the 0b at the beginning

>>> '{0:0b}'.format(10)
'1010'

This question will be helpful to you Converting integer to binary in python

Upvotes: 1

user2555451
user2555451

Reputation:

The 0b means it is binary. And, to remove it, simply do this:

>>> bin(10)
'0b1010'
>>> bin(10)[2:]
'1010'
>>> bin(12345)
'0b11000000111001'
>>> bin(12345)[2:]
'11000000111001'
>>>

This solution takes advantage of Python's slice notation.

Upvotes: 4

Inbar Rose
Inbar Rose

Reputation: 43437

I am not sure exactly what you are asking, but I think its "How do i trim the start of a string off"

And you can do that with slicing:

>>> s = "I am a string"
>>> s[5:]
'a string'

So in your case it would be:

>>> bin(10)[2:]
'1010'

Upvotes: 2

Related Questions