1

This works:

stripped_str = whatever_str.rstrip("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

but just seems very inelegant to me. Any cleaner way of doing it?

Tom Grant
  • 418
  • 4
  • 15
  • this is a vague question. definitions of "clean" and "elegant" are subjective, and they often depend on how the code is going to be used. – akonsu Oct 24 '14 at 17:38
  • http://stackoverflow.com/questions/1450897/python-removing-characters-except-digits-from-string – heri0n Oct 24 '14 at 17:38
  • @heri0n that's not related... this is asking how to strip from the *end* of the string, not how to remove character *anywhere* in the string... – Jon Clements Oct 24 '14 at 17:41

1 Answers1

7

Perhaps you are looking for string.ascii_letters:

from string import ascii_letters
stripped_str = whatever_str.rstrip(ascii_letters)

It allows you to do the same as your current code, but without typing the entire alphabet.

Below is a demonstration:

>>> from string import ascii_letters
>>> ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>>
>>> '123abdjihdkffyifbgh'.rstrip(ascii_letters)
'123'
>>>