Here we use a method using str.join() , str.isnumeric() and a generator expression that will work in 3.x:
>>> my_str = '123Hello, World!4567' >>> output = ''.join(c for c in my_str if not c.isnumeric()) >>> print(output) Hello, World! >>>
This will also work in 2.x if you use the unicode line:
>>> my_str = u'123Hello, World!4567' >>> output = ''.join(c for c in my_str if not c.isnumeric()) >>> print(output) Hello, World! >>>
Hm. Throw it in a paper clip and we will have a MacGyver episode.
Update
I know this was closed as a duplicate, but here is a method that works for both Python 2 and Python 3:
>>> my_str = '123Hello, World!4567' >>> output = ''.join(map(lambda c: '' if c in '0123456789' else c, my_str)) >>> print(output) Hello, World! >>>
source share