python3 str.startswith()_endswith().py

"""
模块:python3 str.startswith()_endswith().py
功能:python3 判断字符串是否以某个字串开始、结束。
参考:
https://www.runoob.com/python3/python3-string-startswith.html
https://www.runoob.com/python3/python3-string-endswith.html
"""
# 1.str.startswith(prefix[, begindex=0[,endindex=len(string)]]) -> True/False
# 检查字符串是否是以指定子字符串开头,
# 如果是则返回 True,否则返回 False。
# 如果参数 begindex 和 endindex 指定值,则在指定范围内检查。
# str -- 要检测的字符串。
# prefix -- 指定的子字符串。
# begindex -- 可选参数,用于设置字符串检测的起始位置。
# endindex -- 可选参数,用于设置字符串检测的结束位置。
str1 = "this is string example....wow!!!"
print(str1.startswith('this'))
# True
# str1[8:]字符串是否以 string 开头
print("str1[8:]:", str1[8:])
# str1[8:]: string example....wow!!!
print(str1.startswith('string', 8))
# True
# str1[2:4]字符串是否以 this 开头
print("str1[2:4]:", str1[2:4])
# str1[2:4]: is
print(str1.startswith('this', 2, 4))
# False

# 2.str.endswith(suffix[, start[, end]]) -> True/False
# 判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回 True,否则返回 False。
# 可选参数 "start" 与 "end" 为检索字符串的开始与结束位置。
# suffix -- 该参数可以是一个字符串或者是一个元素。
# start -- 字符串中的开始位置。
# end -- 字符串中结束位置(不包括该位置的字符)。
# 如果字符串含有指定的后缀返回 True,否则返回 False。
print("\n2.")
Str = 'Runoob example....wow!!!'
suffix = '!!'
print(Str.endswith(suffix))
# True
print(Str.endswith(suffix, 20))
# True
suffix = 'Run'
print(Str.endswith(suffix))
# False
print("Str[0:3]:", Str[0:3])
# Str[0:3]: Run
print(Str.endswith(suffix, 0, 3))
# True


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值