Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6712261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:14:19+00:00 2026-05-26T08:14:19+00:00

I am trying to write a program that will accept user input which will

  • 0

I am trying to write a program that will accept user input which will be a pasted block of text (including multiple newlines/carriage-returns). I’m having trouble finding info on how (if) Python can handle this. Normal behavior is for the input command to complete as soon as the first \n is encountered.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:14:19+00:00Added an answer on May 26, 2026 at 8:14 am

    When I first saw your question I read “input command” as “the command being input”, not as “the input() function”. I’m now assuming here that you’re gathering data from the command line.

    The problem with taking input with newlines in it is: when do you stop accepting input? The following example gets around this by waiting for the user to hit ctrl-d. This triggers an exception in the raw_input() function and then breaks out of the while loop.

    text = ''
    
    # keep looping forever
    while True:
        try:
            # grab the data from the user, and add back the newline
            # which raw_input() strips off
            text += raw_input() + "\n"
        except EOFError:
            # if you encounter EOF (which ctrl-d causes) break out of the loop
            break
    
    # print the text you've gathered after a dashed line
    print "\n------------\n" + text
    

    Obviously you’ll want to warn your user that they’ll have to use ctrl-d to stop entering text, which might be a bit awkward — but if they’re already on the command prompt it shouldn’t be so bad.

    Also, here I’ve used raw_input(), which gathers input but doesn’t exec() it as input() does. If you’re looking to execute the results, you could just replace the print() call with :

    exec(text)
    

    to have similar results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program that will validate a user input consisting of
I'm trying to write a program in Scala, that will accept SOAP-requests, get the
Hey, I'm trying to write a program that will accept new tasks from people,
I am trying to write a small java program that will accept a file
I am trying to write a program that will take the users input value
I am trying to write a fragment program that will take a texture and
Hey there. I'm trying to write a small program that will read the four
I'm trying to write a program that reads text from external file (string string
I'm trying to write a simple program that will receive a string of max
I am trying to write a java program that will automatically download and name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.