Home >Backend Development >Python Tutorial >How Can I Efficiently Capture Multiline Raw Input in Python?
Python offers multiple options for capturing multiline raw input from users. Here are two efficient methods:
In Python 3, input() can be used with a sentinel value to terminate input when a specific string is encountered. Here's an example:
sentinel = '' # ends when this string is seen for line in iter(input, sentinel): # Process each line here
To obtain each line as a string, concatenate the lines using a newline character as a separator:
multi_line_input = '\n'.join(iter(input, sentinel))
For Python 2, use iter(raw_input) instead of iter(input) with the sentinel value method:
multi_line_input = '\n'.join(iter(raw_input, sentinel))
The above is the detailed content of How Can I Efficiently Capture Multiline Raw Input in Python?. For more information, please follow other related articles on the PHP Chinese website!