Sometimes, we want to read multiple lines of raw input with Python.
In this article, we’ll look at how to read multiple lines of raw input with Python.
How to read multiple lines of raw input with Python?
To read multiple lines of raw input with Python, we can use the iter function.
For instance, we write:
sentinel = 'x'
result = '\n'.join(iter(input, sentinel))
print(result)
to call iter with input and sentinel to read in input text until the sentinel string is entered and return a list with the inputted values in a list excluding the sentinel value.
And then we call join to join all the entered text with a new line.
Finally, we assign the list to results.
Conclusion
To read multiple lines of raw input with Python, we can use the iter function.