一般Python编程2.7数据验证

use*_*562 2 python validation

我想知道我应该为这个def函数使用什么函数,以便用户只能输入字符串而不是整数 -

def GetTextFromUser():

    TextFromUser = raw_input('Please enter the text to use: ')

    return TextFromUser
Run Code Online (Sandbox Code Playgroud)

Ash*_*ary 6

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.
Run Code Online (Sandbox Code Playgroud)

例子:

In [9]: 'foo'.isalpha()
Out[9]: True

In [10]: 'foo23'.isalpha()
Out[10]: False
Run Code Online (Sandbox Code Playgroud)