python 字符串-检测

1.isupper() 检测字符串是否都是大写字母组成

1 1 #isupper() 检测字符串是否都是大写字母组成
2 2 str1 = 'YOU HURT MY HEART DEEPLY!1314爱'
3 3 result = str1.isupper()
4 4 print(result)
5 5 #输出结果:True

2.islower() 检测字符串是否都是小写字母组成

1 1 #islower() 检测字符串是否都是小写字母组成
2 2 str1 = 'you hurt my heart deeply!1314爱'
3 3 result = str1.islower()
4 4 print(result)
5 5 #输出结果:True

3.istitle() 检测字符串是否符合标题要求

1 1 #istitle() 检测字符串是否符合标题要求
2 2 title = 'I Love Python'
3 3 result = title.istitle()
4 4 print(result)
5 5 #输出结果:True

4.isalnum() 检测字符串是否由数字和字母及文字组成

1 1 #isalnum() 检测字符串是否由数字和字母及文字组成
2 2 str1 = '我lovepython1314'
3 3 result = str1.isalnum()
4 4 print(result)
5 5 #输出结果:True

5.isalpha() 检测字符串是否由字母和文字组成

1 1 #isalpha() 检测字符串是否由字母和文字组成
2 2 str1 = 'ilovepython%语言'
3 3 result = str1.isalpha()
4 4 print(result)
5 5 #输出结果:False

6.isdigit() 检测字符串是否由10进制字符组成(数字)

1 1 #isdigit() 检测字符串是否由10进制字符组成(数字)
2 2 str1 = '01234567894546'
3 3 result = str1.isdigit()
4 4 print(result)
5 5 #输出结果:True

7.检测字符串是否由数字字符组成(数字)

1 1 #检测字符串是否由数字字符组成(数字)
2 2 str1 = '01234567894546'
3 3 result = str1.isnumeric()
4 4 print(result)
5 5 #输出结果:True

8.isspace() 检测字符串是是否由空白字符(不可见字符)组成

1 1 #isspace()
2 2 str1 = '     \n\r\t'
3 3 print(str1)
4 4 result = str1.isspace()
5 5 print(result)
6 6 #输出结果:True

9.startswith() 检测字符串是否以指定的字符串开头

1 1 #startswith() 检测字符串是否以指定的字符串开头
2 2 str1 = 'python是目前流行排行榜榜首的一种语言!'
3 3 result = str1.startswith('python')
4 4 print(result)
5 5 #输出结果:True

10.endswith()检测字符串是否以指定的字符串结尾

1 1 #endswith()检测字符串是否以指定的字符串结尾
2 2 str1 = 'python是目前流行排行榜榜首的一种语言!'
3 3 result = str1.endswith('')
4 4 print(result)
5 5 #输出结果:True

猜你喜欢

转载自www.cnblogs.com/lws865/p/10843896.html