[Tutor] STRING PROC

Alan Gauld alan.gauld at btinternet.com
Fri May 20 15:55:03 CEST 2011


"Spyros Charonis" <s.charonis at gmail.com> wrote

> A quick string processing query. If I have an entry in a list such 
> as
> ['>NAME\n'],
> is there a way to split it into two separate lines:
>
>>
> NAME

Yes if you know where the linebreak will be.

s = ">NAME\n"
twolines = [s[0],s[1:]]   # list of two strings

for line in twolines; print line

or if you really just want a newline inserted:

twolines = s[0] + '\n' + s[1:]  insert newline

or use replace:

twolines = s.replace( '>' , '>\n' )

If you want more sophistication you could
use  a regex to determine the split point.

hth,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list