Why doesn't str.strip work as I expect?

A beginner who should have asked this here, opened a tracker issue thinking that " ##Python## ".lstrip(' #') should produce "Python## ". In other words, that lstrip always deletes leading whitespace and that the optional chars argument adds to whitespace instead of replacing default whitespace. I repeat below an edited version my answer there.


Use interactive help() to get the answer below.

>>> help(str.lstrip)
Help on method descriptor lstrip:

lstrip(self, chars=None, /) unbound builtins.str method
    Return a copy of the string with leading whitespace removed.

    If chars is given and not None, remove characters in chars instead.

Pass ''.lstrip instead and the first line will instead say “built-in function lstrip”.

The ending, “remove characters in chars instead.” suggests trying

>>> " ##Python## ".lstrip(' #')
'Python## '

which indeed works as desired.

If one enters s.lstrip( in IDLE, and waits for a calltip, it is just the two lines

lstrip(self, chars=None, /)
Return a copy of the string with leading whitespace removed.

which is slightly misleading, but It serves as a reminder of the default and that lstrip only works on the left. Hopefully, one who does not know what non-default chars does will ask help or ask here on Python Discourse.

It does produce that.

There is a typo. Actually, it is:

p=" ##Python## "
print(p.lstrip("#"))

lstrip removes the characters from the string at the left end (treating it like a set), if any appear. "#" doesn’t appear at the left edge of the string, so no deletion occurs.

Also, to avoid transcription errors in the future, it’s best to copy and paste from an actual interactive session, like so:

$ python
Python 3.13.5 (main, Aug 10 2026, 12:06:59) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> " ##Python## ".lstrip('#')
' ##Python## '
>>> " ##Python## ".lstrip(' #')
'Python## '
>>>

Edit: full details https://docs.python.org/3/builtins/stdtypes.html#str.lstrip.