String + number split
Peter Hansen
peter at engcorp.com
Mon Apr 12 14:11:23 EDT 2004
Stevie_mac wrote:
> Hello again, I can do this, but I'm sure there is a much more elegant way...
>
> A string, with a number on the end, strip off the number & discard any underscores
>
> eg...
>
> font12 becomes ('font',12)
> arial_14 becomes ('arial',14)
It's impossible to say if it's more elegant, since you didn't
post your attempt. (Homework? I'll assume not.)
>>> import re
>>> def fsplit(fs):
... return tuple(re.split('(\d+)', fs.replace('_', ''))[:2])
...
>>> fsplit('font12')
('font', '12')
>>> fsplit('arial_14')
('arial', '14')
I wouldn't actually code it this way myself, but it meets
your requirements as stated.
-Peter
More information about the Python-list
mailing list