随笔 - 37  文章 - 0  评论 - 0  阅读 - 18787

Python科研武器库 - 字符串操作 - 字符串开头结尾判断 startswith() endswith()

使用场景:
按照开头的前缀、结尾的后缀来判断、筛选目标字符串

使用函数:

  • str.startswith(search_string, start, end)
  • str.endswith(search_string, start, end)

search_string:要匹配的目标字符串。
start: 要从中匹配search_string的str的起始索引。
end: 要考虑匹配的str的结束索引。

代码示例:

str1 = 'lingdu_for_all'
prefix = 'lingdu'
suffix = 'all'

print(str1.startswith(prefix))
print(str1.startswith(prefix, 6))

print(str1.endswith(suffix))
print(str1.endswith(suffix, 0, 8))

运行结果:

True
False
True
False
posted on   零度的python武器库  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示