splitting a string into 2 new strings

Mikael Olofsson mikael at isy.liu.se
Wed Jul 2 10:08:40 EDT 2003


Hi Mark!

You wrote:
> I have a string e.g. 'C6 H12 O6' that I wish to split up to give 2
> strings 'C H O' and '6 12 6'.  I have played with string.split() and
> the re module - but can't quite get there.

What about this?

import string
t='C6 H12 O6'
theLetters=''.join([x for x in t if x in string.letters+' '])
theDigits=''.join([x for x in t if x in string.digits+' '])

HTH
/Mikael

-----------------------------------------------------------------------
E-Mail:  mikael at isy.liu.se
WWW:     http://www.dtr.isy.liu.se/dtr/staff/mikael               
Phone:   +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339

         /"\
         \ /     ASCII Ribbon Campaign
          X      Against HTML Mail
         / \

This message was sent by Sylpheed.
-----------------------------------------------------------------------
Linköpings kammarkör: www.kammarkoren.com


More information about the Python-list mailing list