python - 通用 Python 编程 2.7 数据验证

标签 python validation

我想知道我应该为这个 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/

相关文章:

python - 如何使用 python 正则表达式在相同模式之间获取行

jquery - MVC 3 客户端对带有数据注释的集合进行验证 - 不起作用

javascript - 如何验证 vue 中的第一个数字不为零?

javascript - angularjs - 输入类型 number/$valid 上的 ng-dirty 类在输入文本时为 true

javascript - AngularJS:输入类型为 "email"的 ngMessage 奇怪行为

python - `matplotlib` : what is the purpose of an artist's animated state?

python - 为什么 Twine 1.9.1 仍在上传到旧版 PyPi?

python tkinter Canvas 不调整大小

python - Spark (2.2) 性能 <-> Spark 持久

javascript - 只允许数字和字母输入字符串