"a//abc".lstrip("a:/") becomes "bc". Why?

Question:

I’ve encountered this now and I can’t understand what’s going on here…:

"a//abc".lstrip("a:/")
# "bc"

Another example:

"a//bcd".lstrip("a:/")
# "bcd"

Does “:” perhaps have a special meaning for the stripping functions?

Asked By: akai

||

Answers:

Because lstrip removes all characters of a set, not a substring

If you want to remove a substring from the start of a string only, I usually do:

# data = "a//abc"
# subs = "a:/"
if data.startswith(subs):
    data = data[len(subs):]
Answered By: Dima Tisnek
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.