How to read multiple lines of raw input?
sentinel = '' # ends when this string is seenfor 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))
Alternatively, you can try sys.stdin.read()
that returns the whole input until EOF
:
import syss = sys.stdin.read()print(s)
Keep reading lines until the user enters an empty line (or change stopword
to something else)
text = ""stopword = ""while True: line = raw_input() if line.strip() == stopword: break text += "%s\n" % lineprint text