[Tutor] Extract lines from a multinile string

John Fouhy john at fouhy.net
Wed Oct 10 06:37:27 CEST 2007


On 10/10/2007, Eli Brosh <ebrosh at nana10.co.il> wrote:
>
>
> Hello
> I have a multiline string such as:
> s='''This is the first line
> This is the second line
> This is the third longer line'''
>
> Note that s has no control character such as \n.

Did you check that?

>>> s = '''This is the first line.
... This is the second line.
... This is the third longer line.'''
>>> print s
This is the first line.
This is the second line.
This is the third londer line.
>>> print repr(s)
'This is the first line.\nThis is the second line.\nThis is the third
longer line.'

A multiline string has newline characters by definition.  You can
split the string around them by using s.split():

>>> s.split('\n')
['This is the first line.', 'This is the second line.', 'This is the
third longer line.']

HTH!

-- 
John.


More information about the Tutor mailing list