How do I read multiple lines of raw input in Python?

felix001 picture felix001 · Jul 26, 2012 · Viewed 54.8k times · Source

I want to create a Python program which takes in multiple lines of user input. For example:

This is a multilined input.
It has multiple sentences.
Each sentence is on a newline.

How can I take in multiple lines of raw input?

Answer

jamylak picture jamylak · Jul 26, 2012
sentinel = '' # ends when this string is seen
for line in iter(raw_input, sentinel):
    pass # do things here

To get every line as a string you can do:

'\n'.join(iter(raw_input, sentinel))

Python 3:

'\n'.join(iter(input, sentinel))