我想在Python中逐个字母读取字符串。在Python3中,我们可以使用 "input()" 逐个读取一个字母吗?
例如:我只想输入一个字母,比如a、b、c,等等。它不应该接受多个字母单词,如ab、as、asa等。在Python 3中是否有这样的函数?
我想在Python中逐个字母读取字符串。在Python3中,我们可以使用 "input()" 逐个读取一个字母吗?
例如:我只想输入一个字母,比如a、b、c,等等。它不应该接受多个字母单词,如ab、as、asa等。在Python 3中是否有这样的函数?
getch
模块。import getch
# ...
char = getch.getch() # User input, but not displayed on the screen
# or
char = getch.getche() # also displayed on the screen
a=input()[0]
print(a)
a将是第一个字符,但如果您想强制用户输入一个字母,可以使用getch
_getch()
。 - Jean-François Fabreinput
可以接受任意数量的字符。你最好的做法就是简单地切掉每个输入字符串的第一个字符。 - Christian Deansys.stdin
中读取一个字符。 - Klaus D.