-1

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

Blythe Simmons
  • 143
  • 3
  • 10

3 Answers3

10

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!@#
vaultah
  • 44,105
  • 12
  • 114
  • 143
khagler
  • 3,996
  • 29
  • 40
  • 3
    this is by far most pythonic answer. Also neet trick if you want to remove all punctuation you can use `s.lstrip(string.punctuation)` – Granitosaurus Jun 24 '15 at 18:51
  • 4
    While this is nice and readable, it may not answer the question as stated. The poster would like to remove all *non-alphanumeric* characters from the start of the string. It is unclear whether it is known that the only non-alphanumeric characters are `!@#` (or in `string.punctuation`, for that matter). A more general solution is to find these first with `leftovers = set(s) - set(string.ascii_letters)`, then strip: `s.lstrip(str(leftovers))`. Though this answer may well be sufficient if `!@#` is all the OP expects. – jme Jun 24 '15 at 18:56
5

You could use a regex matching non-alphanumeric chars at the start of the string:

s = '!@#myname!!'
r = re.compile(r"^\W+") # \W non-alphanumeric at start ^ of string

Output:

In [28]: r = re.compile(r"^\W+")  
In [29]: r.sub("",'!@#myname')
Out[29]: 'myname'    
In [30]: r.sub("",'!@#yourname!@#')
Out[30]: 'yourname!@#'

\W+ will keep underscores so to just keep letters and digits at the start we can:

s = '!@#_myname!!'
r = re.compile(r"^[^A-Za-z0-9]+") 

print(r.sub("",s))
myname!!

If you want to just remove up to the first letter:

r = re.compile(r"^[^A-Za-z]+") 
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
-2

If you want to remove leading non-alpha/numeric values:

while not s[0].isalnum(): s = s[1:]

If you want to remove only leading non-alphabet characters:

while not s[0].isalpha(): s = s[1:]

Sample:

s = '!@#yourname!@#'
while not s[0].isalpha(): s = s[1:]
print(s)

Output:

yourname!@#
vaultah
  • 44,105
  • 12
  • 114
  • 143
Pruthvi Raj
  • 3,016
  • 2
  • 22
  • 36
  • `'1'.isalnum()` -> `True`, `'1'.isalpha()` -> `False` – vaultah Jun 24 '15 at 18:31
  • @vaultah sorry for the confusion, i edited the question after you answered it. I meant non-alphabet characters, not non-alphanumeric characters. Your answer was correct for the original question, my mistake – Blythe Simmons Jun 24 '15 at 18:40
  • 1
    The complexity of this code is awful, with a lot of string copies depending on how many characters are matched. This is not a pythonic way to do this. – birdypme Jun 24 '15 at 19:11
  • 1
    I'm almost afraid to imagine what code would make this solution appear "very elegant" by comparison. – TigerhawkT3 Jun 24 '15 at 19:16
  • 1
    Sorry guys I cannot delete the answer since it's accepted :), I did what I could :) – Pruthvi Raj Jun 24 '15 at 19:18