7

I'm using raw_input() to storing a message inside a variable. So I can't press enter for a carriage return/new line to start a new paragraph. Right now if I press enter it will just proceed to the next portion of my program.

I already tried something like this:

>>> message = raw_input("Message: ")
Message: Hello Sir, \n It's great that..
>>> message
"Hello Sir, \\n It's great that.."
>>> 

It didn't worked, and I also tried enclosing it in single and double quotes, which also didn't worked.

I understand that there are other ways of doing this, like using wxpython or tkinter, but I want to keep it strictly console. Is this possible?

1 Answer 1

7

Can you use the sys module? This will do the trick if you want. Just hit Ctrl-D to end it.

import sys
message = sys.stdin.readlines()

Otherwise, this answers your question: Python raw_input ignore newline

3
  • sys.stdin.readlines() works like a charm, but i can't seem to get ctrl-d to work i don't know why? Commented Apr 20, 2012 at 8:49
  • i figured it out, i have to use ctrl-c instead, but now i get a keyboard interrupt error if i tried to print the message. Commented Apr 20, 2012 at 9:00
  • 1
    I figured it out finally! I can't use ctrl-c, that's what makes the keyboard interrupt error, and I can't use ctrl-d because i'm on windows, i have to use ctrl-z or f6 and then it will display in the console like this ^Z and press enter. after that i have to convert the message from a list back into a string, which i manage to do like this >>>roughmessage = sys.stdin.readlines() >>>message = ''.join(roughmessage) Commented Apr 20, 2012 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.