python字符串处理函数
整理一下最近学习到的python字符处理函数
str.startswith()
startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。
str.startswith(str, beg=0,end=len(string));
str – 检测的字符串。
beg – 可选参数,用于设置字符串检测的起始位置。
end – 可选参数,用于设置字符串检测的结束位置。
例子1:
str = "Fruits here: Apple, Pear, Peach, Watermelon, Apple, Orange"
substr = "Apple"
if(str.startswith("Fruits") and str.count(substr) > 0):
print(str.find(substr))
else:
print("Not fruits category")
输出1:
13
例子2:
str = "Food here: Rice, Fish, Chicken, Apple, Orange juice"
substr = "Apple"
if(str.startswith<