startswith()函数是Python中的一个字符串方法,用于检查字符串是否以指定的子字符串开头,如果是,则返回True,否则返回False。

创新互联专业为企业提供米林网站建设、米林做网站、米林网站设计、米林网站制作等企业网站建设、网页设计与制作、米林企业网站模板建站服务,十多年米林做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
用法:
str.startswith(prefix[, start[, end]])
参数说明:
prefix:要检查的子字符串。
start(可选):从字符串的哪个位置开始检查,默认为0。
end(可选):在字符串的哪个位置结束检查,默认为字符串的长度。
示例:
1、检查字符串是否以指定子字符串开头
text = "Hello, world!"
result = text.startswith("Hello")
print(result) # 输出:True
2、检查字符串是否以指定子字符串开头,并指定起始和结束位置
text = "Hello, world!"
result = text.startswith("Hello", 0, 5)
print(result) # 输出:True
3、检查空字符串是否以指定子字符串开头
text = ""
result = text.startswith("Hello")
print(result) # 输出:False