How would you use Python’s input() to capture a full multi-line paragraph , ending only when the user presses Enter on a blank line?
for example, I want input() to read these lines :
Hello, everyone!
Welcome to RHLC.
We’re excited to learn together.
Hi,
The idea here is to capture all user input until the user presses Enter on a blank line. To achieve this, we can use the input()function inside a loop, where the loop exits once an empty line is entered.
Each non-empty line is appended to a list of result lines. After the loop terminates, the list of lines is joined into a single paragraph string while preserving line breaks.
Below is the Python code that implements this logic:
def capture_multi_line_paragraph():
"""
Captures multi-line input from the user, ending when a blank line is entered.
"""
lines = []
while True:
line = input()
if line == "":
# Break the loop when an empty line (blank line) is received
break
lines.append(line)
# Join the list of lines into a single paragraph string,
# preserving the line breaks
paragraph = "\n".join(lines)
return paragraph
print(capture_multi_line_paragraph())
This code returns a single string containing all the user-entered lines, separated by newline characters, stopping input collection when a blank line is entered.
Hope this could help you, let me know for your feedback.
Regards,
Mohamad Abo Ras
Hi,
The idea here is to capture all user input until the user presses Enter on a blank line. To achieve this, we can use the input()function inside a loop, where the loop exits once an empty line is entered.
Each non-empty line is appended to a list of result lines. After the loop terminates, the list of lines is joined into a single paragraph string while preserving line breaks.
Below is the Python code that implements this logic:
def capture_multi_line_paragraph():
"""
Captures multi-line input from the user, ending when a blank line is entered.
"""
lines = []
while True:
line = input()
if line == "":
# Break the loop when an empty line (blank line) is received
break
lines.append(line)
# Join the list of lines into a single paragraph string,
# preserving the line breaks
paragraph = "\n".join(lines)
return paragraph
print(capture_multi_line_paragraph())
This code returns a single string containing all the user-entered lines, separated by newline characters, stopping input collection when a blank line is entered.
Hope this could help you, let me know for your feedback.
Regards,
Mohamad Abo Ras
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.