splitting a string into 2 new strings

Gerhard Häring gh at ghaering.de
Wed Jul 2 14:06:34 CEST 2003


Mark Light wrote:
> Hi,
>       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.

So what's the rule for splitting? Always split on position 4? That'd be 
too easy, I suppose:

 >>> s = "C6 H12 O
 >>> s[:4], s[4:]
('C6 H', '12 O6')

-- Gerhard





More information about the Python-list mailing list