Most Pythonic was to strip all non-alphanumeric leading characters from string

Blythe Simmons picture Blythe Simmons · Jun 24, 2015 · Viewed 7.4k times · Source

For example

!@#123myname --> myname
!@#yourname!@#123 --> yourname!@#123

There are plenty of S.O. examples of "most pythonic ways of removing all alphanumeric characters" but if I want to remove only non-alphabet characters leading up to first alphabet character, what would be the best way to do this?

I can do it with a while loop but im looking for a better python solution

Answer

khagler picture khagler · Jun 24, 2015

Just use str.lstrip.

It takes a string containing the characters to remove from the left side of the string, and will remove those characters regardless of the order in which they appear. For example:

s = "!@#yourname!@#"
print s.lstrip('@!#') # yourname!@#