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)

wim
  • 338,267
  • 99
  • 616
  • 750
Harryooo
  • 123
  • 6
  • 2
    [Reading documentation helps](http://docs.python.org/library/stdtypes.html#str.lstrip) ;) : *The chars argument is not a prefix; rather, all combinations of its values are stripped.* – Felix Kling Nov 10 '10 at 20:59
  • Similar to Why does str.lstrip strips an extra character? http://stackoverflow.com/questions/1687171/why-does-str-lstrip-strips-an-extra-character – Steven Rumbalski Nov 10 '10 at 21:08

7 Answers7

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:]
John Machin
  • 81,303
  • 11
  • 141
  • 189
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.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
6

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

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

AXO
  • 8,198
  • 6
  • 62
  • 63
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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Yes, but why only the first "1" in the second case? – Harryooo Nov 10 '10 at 23:05
  • @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. – Mark Ransom Nov 11 '10 at 03:34
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!

winwaed
  • 7,645
  • 6
  • 36
  • 81
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.

Thomas K
  • 39,200
  • 7
  • 84
  • 86
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.