cancel
Showing results for 
Search instead for 
Did you mean: 
Chetan_Tiwary_
Community Manager
Community Manager
  • 425 Views

Python list of inputs

Jump to solution

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. 
Labels (3)
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
  • 79 Views

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

View solution in original post

Tags (1)
1 Reply
  • 80 Views

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

Tags (1)
Join the discussion
You must log in to join this conversation.