Extract the numeric and alphabetic part from an alphanumeric string

alex23 wuwei23 at gmail.com
Mon Aug 3 18:40:02 CEST 2009


Sandhya Prabhakaran <sandhyaprabhaka... at gmail.com> wrote:
> I have a string as str='123ACTGAAC'.

You shouldn't use 'str' as a label like that, it prevents you from
using the str() function in the same body of code.

> How do I blank out the initial numeric part so as to get just the
> alphabetic part. The string is always in the same format.

>>> sample = '123ACTGAAC'
>>> numer = ''.join(s for s in sample if s.isdigit())
>>> alpha = ''.join(s for s in sample if s.isalpha())
>>> numer, alpha
('123', 'ACTGAAC')

If by 'always in the same format' you mean the positions of the
numbers & alphas,
you could slightly abuse the struct module:

>>> import struct
>>> sample = '123ACTGAAC'
>>> format = '3s7s' # a string of 3 + a string of 7
>>> struct.unpack(format, sample)
('123', 'ACTGAAC')

But seriously, you should use slicing:

>>> sample = '123ACTGAAC'
>>> sample[0:3], sample[3:]
('123', 'CTGAAC')

You can also label the slices, which can be handy for self-documenting
your code:

>>> num = slice(3)
>>> alp = slice(4,10)
>>> sample[num], sample[alp]
('123', 'CTGAAC')



More information about the Python-list mailing list