python str使用笔记(更新)

时间:2023-03-09 03:13:29
python str使用笔记(更新)

判断字符串是否以某个串为结尾:

str.endswith(strtmp)  返回True/False

 >>> strs='aba'
>>> strs.endswith('ba')
True
>>> strs.endswith('b')
False

检查某字符串是否为另一字符串的子串:

str.find(strtmp)方法  返回首次出现的下标位置,不存在则返回-1

 >>> strs='aba'
>>> strs.find('b')
1
>>> strs.find('a')
0
>>> strs.find('c')
-1

in使用  strtmp in str  返回True/False

 >>> strs='aba'
>>> 'b' in strs
True
>>> 'a' in strs
True
>>> 'c' in strs
False