0

I was trying to strip a string like

var/vob/bxxxxx/xxxxx/vob

I am doing it like...

'var/vob/bxxxxx/xxxxx/vob'.lstrip('/var/vob/')

I am expecting an output like ...

bxxxxx/xxxx/vob

But it is only giving...

xxxx/xxxxxx/vob

Yes I know because of the first letter is b and in python prefix b for a string stands for converting it into bytes, and I have read this too...

But what I wanted to know is how to bypass this thing.. I want to get the desired output...

I would love to say the things I have tried.. but I don't find any way around to try... can some one throw some light on this...

Thanks :)

Community
  • 1
  • 1
Sravan K Ghantasala
  • 1,058
  • 8
  • 14
  • 1
    Your sample string doesn't even begin with your `lstrip` argument, since it lacks the leading `/`. If you want to chop a fixed prefix off a string, I'd test `startswith(prefix)` and then slice, something like `test[len(prefix):]`. – Peter DeGlopper Dec 19 '13 at 04:20
  • Oh, and in response to this part: "in python prefix b for a string stands for converting it into bytes" - that refers to a) only Python 3.x and b) refers to using `b` as a marker outside the quotes. In Python 3, this would be a literal for a `bytes` object: `b'foo'`, while this would be a literal for a string object: `'bfoo'`. In Python 2.x, the `u` marker serves an inverse purpose - `'foo'` is an encoded string, while `u'foo'` is a Unicode object. – Peter DeGlopper Dec 19 '13 at 04:28

3 Answers3

6

You misunderstand what lstrip does. It removes all characters that are part of the parameter string. Order doesn't matter. Since b is in the string it is removed from the front of the result.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
2

What about

if s.startswith("var/vob/):
    s = s[8:]

And yes, Mark is right, lstrip removes any haracters from contained in the argument from the string.

bigblind
  • 12,539
  • 14
  • 68
  • 123
2

The best way to do this would be, like this

data, text = "/var/vob/bxxxxx/IT_test/vob", "/var/vob/"
if data.startswith(text): data = data[len(text):]
print data

Output

bxxxxxx/IT_test/vob
Sravan K Ghantasala
  • 1,058
  • 8
  • 14
thefourtheye
  • 233,700
  • 52
  • 457
  • 497