NOTE: Under Construction - I'm in the middle of upgrading my site and lots of stuff is kinda broken. Please forgive the mess.

Remove Arbitrary Leading Character From A Python String With .lstrip()

Passing a string of characters to .lstrip() will remove those characters from the start of the string being operated on. For example:

October 2023
alfa = f"""  a  b  a

x a charlie"""

stripped = alfa.lstrip("ab \n")

START|x a charlie|END


  • This example passes a string with four characters to `.lstrip()`: "a", "b", " " (space), and "\n" (newline)

  • Any number of the given characters in any different combination are removed

  • As soon as a character that's not in the argument is seen, no further removal is done (hence the single "a" character in the example above remaining since it's behind the "x"

  • See also .removeprefix() for how to remove a specific string instead of a set of characters

═══ § ═══

Footnotes And References