3

Is there an python function similar to raw_input but that doesn't show a newline when you press enter. For example, when you press enter in a Forth prompt it doesn't show a newline.
Edit:
If I use the code:

data = raw_input('Prompt: ')
print data

than the output could be:

Prompt: Hello
Hello

because it printed a newline when I pressed enter. I want a function similar to raw_input that doesn't show the newline. So if the function I wanted was called special_input and I used the code:

data = special_input('Prompt: ')
print data

than the output would be something like:

Prompt: Hello Hello
None
  • 3,875
  • 7
  • 43
  • 67
  • I'm not sure about raw_input, but the print function won't include a newline if you use a comma after e.g ( print "Hello",) – Decio Lira Jun 23 '10 at 22:46
  • In Python versions under 3, print isn't a function, it's a keyword, so adding a comma changes things because it has a special syntax. raw_input is a function, so it doesn't have a special syntax that a keyword can have. – None Jun 23 '10 at 23:45
  • possible duplicate of [How to print in Python without newline or space?](http://stackoverflow.com/questions/493386/how-to-print-in-python-without-newline-or-space) – Seth Battin Apr 17 '14 at 16:38

1 Answers1

3

Yes, there are other ways to read a line like raw_input

You can use sys.stdin():

import sys
line = sys.stdin.readline()

Or if you want to get a password you can also use getpass.getpass():

import getpass
line = getpass.getpass()

But if you want something more fancy you will need to use curses

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
razpeitia
  • 1,947
  • 4
  • 16
  • 36