Python3.7知其然知其所以然-第六章 字符串( 八 )

# 字符串内容

s = \"Welcome to Python.\"

print(s.index(\"a\"))

print(s.index(\"c\"))

# 从指定范围进行查找

print(s.index(\"e\" 5 7))

(3)判断是否是指定的字符串开头 , 是的话 , 返回True , 否则返回False 。 可以从指定的范围进行查找它的语法是:str.startswith(str beg=0end=len(string)) 。

# 字符串内容

s = \"I am a handsome boy.\"

# 找到返回True

print(s.startswith(\"I\"))

# 从指定范围查找 , 找不到 , 返回False

print(s.startswith(\"I\" 2 6))

# 没找到返回False?

推荐阅读