Python3 笔记:字符串的 startswith() 和 endswith()

1、startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定了值,则在指定范围内检查。

语法:str.startswith(substr, beg=0,end=len(string))

参数:
str -- 检测的字符串。
substr -- 指定的子字符串。
strbeg -- 可选参数用于设置字符串检测的起始位置。
strend -- 可选参数用于设置字符串检测的结束位置。

str1 = 'this is a test'
print(str1.startswith('this'))	# 运行结果:True
print(str1.startswith('this',2))	# 运行结果:False
print(str1.startswith('this',0,6))	# 运行结果:True

2、endswith() 方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回 True,否则返回 False。可选参数 "start" 与 "end" 为检索字符串的开始与结束位置。

语法:str.endswith(suffix[, start[, end]])

参数
suffix -- 该参数可以是一个字符串或者是一个元素。
start -- 字符串中的开始位置。
end -- 字符中结束位置。

str1 = 'this is a test'
print(str1.endswith('this'))	# 运行结果:False
print(str1.endswith('test',2))	# 运行结果:True
print(str1.endswith('this',0,9))	# 运行结果:False
print(str1.endswith('this',0,4))	# 运行结果:True

相关推荐

  1. Python3 笔记字符串 startswith() endswith()

    2024-06-09 12:00:04       27 阅读
  2. Python3 笔记字符串 replace() expandtabs()

    2024-06-09 12:00:04       17 阅读
  3. Python3 笔记字符串 zfill() rjust()

    2024-06-09 12:00:04       35 阅读
  4. Python3 笔记字符串 encode() bytes.decode()

    2024-06-09 12:00:04       30 阅读
  5. Python3 笔记字符串 strip()、lstrip()、rstrip()

    2024-06-09 12:00:04       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-09 12:00:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-09 12:00:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-09 12:00:04       82 阅读
  4. Python语言-面向对象

    2024-06-09 12:00:04       91 阅读

热门阅读

  1. 数据库与低代码开发:技术革新与应用实践

    2024-06-09 12:00:04       36 阅读
  2. 数据仓库中常用的元数据管理系统

    2024-06-09 12:00:04       26 阅读
  3. LeetCode 9 - 回文数

    2024-06-09 12:00:04       30 阅读
  4. Git 和 Github 的使用

    2024-06-09 12:00:04       23 阅读
  5. 描述Hibernate的映射文件和配置文件的作用

    2024-06-09 12:00:04       32 阅读
  6. less 基础语法

    2024-06-09 12:00:04       29 阅读
  7. Visual 怎么编程:一场视觉与逻辑的奇妙旅程

    2024-06-09 12:00:04       31 阅读
  8. Linux | 标准IO编程

    2024-06-09 12:00:04       30 阅读