[Tutor] How do I save the output of the rotor module?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Nov 7 13:04:29 EST 2003

> The code now decrypts successfully. Although it leaves the file in the
> form of one large string instead of a list of strings. The split()
> method returns it to a list of strings but *removes* the \n's. I could
> loop through the list and append newlines to each string. Can split() be
> instructed to perform the split without removing the newlines?

Hi Mike,

split() can't, but splitlines() can.

>>> s = "hello world\nthis is a test\n"
>>> s.split()
['hello', 'world', 'this', 'is', 'a', 'test']
>>> s.splitlines()
['hello world', 'this is a test']
>>> s.splitlines(True)
['hello world\n', 'this is a test\n']

splitlines() can take in an optional 'keepends' parameter: if we send it a
true value, then it'll keep the newlines.

We can find out more about the string methods in the Library Reference:


I hope this helps!

More information about the Tutor mailing list