python如何判断字符是否为字母

Python (161) 2023-06-01 17:36:43

Python中有一个方法isalpha(),该方法可以检测字符串是否只由字母组成。如果字符串至少有一个字符并且所有字符都是字母则返回 True,否则返回 False。

(推荐教程:Python入门教程)

语法:

str.isalpha()

代码示例:

#!/usr/bin/python
#coding=utf-8

str="php";
printstr.isalpha();

str="php中文网";
printstr.isalpha();

str="thisisstringexample....wow!!!";
printstr.isalpha();

输出结果:

True
False
False
THE END

发表回复