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 :)

2
  • 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):]. Commented Dec 19, 2013 at 4: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. Commented Dec 19, 2013 at 4:28

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@PeterDeGlopper, that comment would be better left on the original question.
Fair enough - I thought of it as expanding your answer (which explains why the OP's attempt didn't work) by suggesting an alternate approach, but no matter.
Thanks for enlightening me :)
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.

Comments

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

Comments

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.