[Tutor] manipulating a string

Alan Gauld alan.gauld at btinternet.com
Tue Jul 15 09:53:01 CEST 2008

"Bryan Fodness" <bryan.fodness at gmail.com> wrote

>I have a string (column names) that I need to split.
> D_H = 'D 5 10 15 20 25 30 35 40 D Upper D Lower'

I assume you are reading this from a file or network and therefore
don't actually know in advance what the list contains?
(Otherwise just do it manually!)

> I cannot do a simple list(D_H).split because the last two strings 
> have a
> space between them (D Upper and D Lower are two, not four labels).

If the format is constant and it is always the last 2 fields that
have that format then just split into a list then combine the last
two elements. If you don;t know which elements will contain
spaces then you are sunk I think because there is no obvious
data pattern to determine which items should be joined. Or is there,
but you haven't told us - like the second word always being a
string rather than a letter/number?

We may need more information.

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list