Home  >  Article  >  Backend Development  >  python中常用检测字符串相关函数汇总

python中常用检测字符串相关函数汇总

WBOY
WBOYOriginal
2016-06-10 15:15:061140browse

本文实例汇总了python中常用检测字符串相关函数。分享给大家供大家参考。具体分析如下:

下面的python代码可用于检测字符串,包括是否全部为数字,是否包含数字,是否包含标题单词,是否包含大写字母,是否包含小写字母,是否包含空格,是否以指定的字符开头和结尾。

my_string = "Hello World"
my_string.isalnum()   #检测所有字符是否都是数字
my_string.isalpha()   #检测字符串中的所有字符是否都是字母
my_string.isdigit()   #检测字符串是否包含数字
my_string.istitle()   #检测字符串是否包含标题单词
my_string.isupper()   #检测字符串是否包含大写字母
my_string.islower()   #检测字符串是否包含小写字母
my_string.isspace()   #检测字符串是否包含空格
my_string.endswith('d')   #检测字符串是否以字符d结束
my_string.startswith('H')  #检测字符串是否以大写字母H开头

下面显示返回结果

my_string="Hello World"
print my_string.isalnum()    #False
print my_string.isalpha()    #False
print my_string.isdigit()    #False
print my_string.istitle()    #True
print my_string.isupper()    #False
print my_string.islower()    #False
print my_string.isspace()    #False
print my_string.endswith('d')    #True
print my_string.startswith('H')   #True

希望本文所述对大家Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn