5

Trying to strip the "0b1" from the left end of a binary number.

The following code results in stripping all of binary object. (not good)

>>> bbn = '0b1000101110100010111010001' #converted bin(2**24+**2^24/11)
>>> aan=bbn.lstrip("0b1")  #Try stripping all left-end junk at once.
>>> print aan    #oops all gone.
''

So I did the .lstrip() in two steps:

>>> bbn = '0b1000101110100010111010001' #    Same fraction expqansion
>>> aan=bbn.lstrip("0b")# Had done this before.
>>> print aan    #Extra "1" still there.
'1000101110100010111010001'
>>> aan=aan.lstrip("1")#  If at first you don't succeed...
>>> print aan    #YES!
'000101110100010111010001'

What's the deal?

Thanks again for solving this in one simple step. (see my previous question)

2

7 Answers 7

12

The strip family treat the arg as a set of characters to be removed. The default set is "all whitespace characters".

You want:

if strg.startswith("0b1"):
   strg = strg[3:]
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, but why only the first "1" in the second case?
@Harryooo: A question for you: Why does Python have both an lstrip and a rstrip and also a strip that combines lstrip and rstrip functionality?
10

No. Stripping removes all characters in the sequence passed, not just the literal sequence. Slice the string if you want to remove a fixed length.

3 Comments

For example, "ababbbbbbaaccc".lstrip("ab") => "ccc"
I sort of figured that lstrip() was using char as a list, but then I was surprised again when lstrip("1") only eliminated the first "1" and not the rest.
Image
That's because there was only a single 1 on the left.
8

In Python 3.9 you can use bbn.removeprefix('0b1').

(Actually this question has been mentioned as part of the rationale in PEP 616.)

Comments

0

This is the way lstrip works. It removes any of the characters in the parameter, not necessarily the string as a whole. In the first example, since the input consisted of only those characters, nothing was left.

2 Comments

Yes, but why only the first "1" in the second case?
@Harryooo, in your second example lstrip("1") removes characters on the left until it reaches something that isn't a 1. If it had started with '1111' it would have removed all of them.
0

Lstrip is removing any of the characters in the string. So, as well as the initial 0b1, it is removing all zeros and all ones. Hence it is all gone!

1 Comment

Yes, but why only the first "1" in the second case?
0

@Harryooo: lstrip only takes the characters off the left hand end. So, because there's only one 1 before the first 0, it removes that. If the number started 0b11100101..., calling a.strip('0b').strip('1') would remove the first three ones, so you'd be left with 00101.

Comments

0
>>> i = 0b1000101110100010111010001
>>> print(bin(i))
'0b1000101110100010111010001'

>>> print(format(i, '#b'))
'0b1000101110100010111010001'
>>> print(format(i, 'b'))
'1000101110100010111010001'

See Example in python tutor:

From the standard doucmentation (See standard documentation for function bin()): bin(x) Convert an integer number to a binary string prefixed with “0b”. The result is a valid Python expression. If x is not a Python int object, it has to define an index() method that returns an integer. Some examples:

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'

If prefix “0b” is desired or not, you can use either of the following ways.

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')

See also format() for more information.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.