首页 › 参考手册 › 字符串方法 › str.startswith()

Python str.startswith() 方法

描述

判断字符串是否以指定前缀开头。prefix 可以是元组,表示"以其中任意一个开头"。endswith 用法相同。

语法

str.startswith(prefix[, start[, end]])

参数

参数说明
prefix前缀字符串或元组

返回值

布尔值

实例

url = "https://example.com"
print(url.startswith("https"))
print("photo.JPG".lower().endswith((".jpg", ".png")))
以上实例输出结果为:
True
True