Python input that ends without showing a newline

和自甴很熟 提交于 2019-12-07 14:29:45

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/3105971/python-input-that-ends-without-showing-a-newline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!