Python3 中的 startswith(str,beg=0,en

首页 / Python3入门教程 / Python3 中的 startswith(str,beg=0,en

Python startswith()方法返回True或False。如果字符串以前缀开头,则返回True,否则返回False。它需要两个参数start和end。 Start是开始搜索的起始索引,end索引是搜索终止的位置。

startswith - 语法

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

startswith - 参数

prefix - 要检查的字符串。

start    - 从搜索开始的地方开始索引。

end      -  结束索引,直到执行搜索为止。

开始和结束都是可选参数。

无涯教程网

startswith - 返回

它返回布尔值True或False。

首先创建一个简单的示例,如果字符串以前缀开头,则输出True。

# Python String startswith() method
#声明变量
str = "Hello Learnfk"
# 调用函数
str2 = str.startswith("Hello")
# 显示结果
print (str2)

输出

True

如果字符串不是以前缀开头,则该方法返回False。参见下面的例子

# Python String startswith() method
# 声明变量
str = "Hello Learnfk"
# 调用函数
str2 = str.startswith("Java") # False
# 显示结果
print (str2)

输出

False

此方法采用三个参数。开始索引和结束索引是可选的。在这里,无涯教程仅传递起始索引。

# Python String startswith() method
# 声明变量
str = "Hello Learnfk"
# 调用函数
str2 = str.startswith("Lear",6)
# 显示结果
print (str2)

输出

True

如果字符串位于起始索引和终止索引之间,并且从前缀开始,则返回true。创建一个示例来描述该过程。

# Python String startswith() method
# 声明变量
str = "Hello Learnfk"
# 调用函数
str2 = str.startswith("Lear",6,10)
# 显示结果
print (str2)
str2 = str.startswith("Lear",8,12)
# 显示结果
print (str2)

输出

True
False

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

持续交付36讲 -〔王潇俊〕

重学前端 -〔程劭非(winter)〕

设计模式之美 -〔王争〕

SRE实战手册 -〔赵成〕

手机摄影 -〔@随你们去〕

性能优化高手课 -〔尉刚强〕

数据分析思维课 -〔郭炜〕

遗留系统现代化实战 -〔姚琪琳〕

快速上手C++数据结构与算法 -〔王健伟〕

好记忆不如烂笔头。留下您的足迹吧 :)