我想知道我应该为这个 def 函数使用什么函数,以便用户只能输入字符串而不是整数 -
def GetTextFromUser():
TextFromUser = raw_input('Please enter the text to use: ')
return TextFromUser
最佳答案
raw_input()
总是返回一个字符串。但是,如果按字符串表示只允许使用字母,则可以使用:str.isalpha()
S.isalpha() -> bool
Return True if all characters in S are alphabetic
and there is at least one character in S, False otherwise.
例子:
In [9]: 'foo'.isalpha()
Out[9]: True
In [10]: 'foo23'.isalpha()
Out[10]: False
关于python - 通用 Python 编程 2.7 数据验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15230099/