美文网首页
【18】python第十八--字符串判断语法

【18】python第十八--字符串判断语法

作者: 咗嚛 | 来源:发表于2020-03-22 22:01 被阅读0次

字符串判断即是判断真假,返回的结果是布尔型数据类型: True 或False。

●startswith(): 检查字符串是否是以指定子串开头,是则返回True, 否则返回False。 如果设置开始和结束位置下标,则在指定范围内检查。
1.语法
1字符串序列. startswith(子串, 开始位置下标,结束位置下标)
mystr = "hello world and itcast and itheima and Python
如: print (mystr. startswith( 'hello'))
结果: True

●endswith(子串, 开始位置下标,结束位置下标)
检查字符串是否是以指定子串结尾,是则返回True, 否则返回False。

●isalpha(): 如果字符串至少有-个字符并且所有字符都是字母则返回True,否则返回False。
mystr1 = 'hello'
mystr2 = ' hello12345 '

结果:True

print(mystr1. isalpha())

结果: False

●isdigit(): 如果字符串只包含数字则返回True否则返回False。

相关文章

网友评论

      本文标题:【18】python第十八--字符串判断语法

      本文链接:https://www.haomeiwen.com/subject/wsjbyhtx.html