Extract the numeric and alphabetic part from an alphanumeric string

Andreas Tawn andreas.tawn at ubisoft.com
Mon Aug 3 11:33:00 EDT 2009


> Hi,
> 
> I have a string as str='123ACTGAAC'.
> 
> I need to extract the numeric part from the alphabetic part which I
> did using
> >>>numer=re.findall(r'\d+',str)
> >>>numer
> 123
> 
> To get the alphabetic part, I could do
> >>>alpha=str.replace('123','')
> >>>alpha
> ACTGAAC
> But when I give
> >>>alpha=str.replace(numer,'')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: expected a character buffer object
> 
> 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.
> 
> Please help.
> 
> Regards,
> Sandhya

If the format's always the same, you could use slicing instead.

>>> s = '123ACTGAAC'
>>> s[:3]
'123'
>>> s[3:]
'ACTGAAC'

BTW, you should avoid using built-ins like str for variable names. Bad
things will happen.

Cheers,

Drea



More information about the Python-list mailing list