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:

Code
alfa = f"""  a  b  a

x a charlie"""

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

print(f"START|{stripped}|END")
Results
START|x a charlie|END

Details

  • 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