Python输入结束而不显示换行符 [英] Python input that ends without showing a newline
问题描述
是否存在类似于raw_input的python函数,但是按Enter键时不会显示换行符.例如,当您在Forth提示中按Enter时,它不会显示换行符.
如果我使用代码:
data = raw_input('Prompt: ')
print data
比输出可能是:
Prompt: Hello
Hello
因为它在我按Enter键时打印了换行符.我想要一个类似于raw_input的函数,不显示换行符.因此,如果我想要的函数称为special_input,并且使用了代码:
data = special_input('Prompt: ')
print data
比输出类似:
Prompt: Hello Hello
是的,还有其他方法可以读取raw_input
您可以使用 sys.stdin()
:
import sys
line = sys.stdin.readline()
或者,如果您想获取密码,也可以使用 getpass.getpass()
:
import getpass
line = getpass.getpass()
但是,如果您想要更精美的东西,则需要使用诅咒 >
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
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
这篇关于Python输入结束而不显示换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!