我不知道如何将用户输入作为字符串处理

I can't figure out how to handle user input as strings

我是 Python 的新手,所以我不太了解语法。我正在尝试编写一个程序,该程序接受单独行中的单词并将每个单词添加到数组中。输入所有单词后,我尝试使用 isalpha() 来打破循环。但是即使是一个词,循环也会中断。请帮忙!编辑:我还想知道如何去掉输出中的 \n。

import sys; args = sys.argv[1:]
import fileinput
words = []
for line in fileinput.input():
   if line.isalpha() == False:
    fileinput.close()
   words.append(line)
print (words)

输入:你好

输出:['hello\n']

来自the documentation for isalpha

Return True if all characters in the string are alphabetic and there is at least one character, False otherwise.

IPython中的一些工作:

In [1]: 'hello\n'.isalpha()
Out[1]: False

In [2]: 'hello\n'.rstrip().isalpha()
Out[2]: True

我会用这样的方法解决眼前的问题:

for line in fileinput.input():
    line = line.rstrip()
    if line.isalpha() == False:
        fileinput.close()
    words.append(line)