python字符串endswith,Python字符串| 具有示例的endswith()方法

Python字符串endswith()方法

endswith()Method是Python中的一种库方法,用于检查字符串是否以给定的后缀(子字符串)结尾。它返回True –如果字符串以给定的后缀结尾,否则返回False。

语法:

String.endswith(suffix, start, end)

Parameter(s):后缀:这可能是子字符串或我们在字符串中查找的元组。

start:它是方法中的一个可选参数,endswith()默认值为0。它是字符串中我们要开始检查后缀的起点。

end:它也是方法中的可选参数,endswith()默认值为-1。它在字符串中定义了需要后缀的终点,即直到我们要找到后缀的位置。

返回值:

如果字符串以给定的后缀结尾,则返回True,否则返回false。

Python代码演示方法示例String.endswith()

# 搜索“编程容易”字符串搜索“编程容易”字符串没有开始和结束参数的endswith()

print('endswith() Without start and end parameters')

string1='NHOOO is the Best Technical Content Website.'

suffix='Website.'

#返回True

print('When no optional parameter is passed:',string1.endswith(suffix))

#将False改成'。'。不包括在内

print('When no optional parameter is passed:',string1.endswith('Website'))

#可能有多个单词,但只有一个字符串。

print('When large string is to be found:',string1.endswith('Technical Content Website.'))

print()

print()

print()

# 搜索“编程容易”字符串搜索“编程容易”字符串endwith()具有开始和结束参数

print('endswith() With start and end Parameters')

# 搜索“编程容易”字符串搜索“编程容易”字符串提供开始和结束

# 搜索“编程容易”字符串搜索“编程容易”字符串开始:42,结束:51

# 搜索“编程容易”字符串"programming is easy" string is searched

#返回True

print('When optional parameter is passed:',string1.endswith(suffix,42,51))

#将False改成'。'。不包括在内

print('When optional parameter is passed:',string1.endswith('Website',43,52))

#可能有多个单词,但只有一个字符串。

print('When large string is to be found with optional parameters:',string1.endswith('Technical Content Website.',42,51))

print()

print()

print()

#endwith()带元组后缀

print('endswith() With Tuple Suffix')

#返回True

print('When tuple is passed without optional parameters:',string1.endswith(('content','Website.','Includehelp')))

#恢复真实

print('When tuple is passed with optional parameters:',string1.endswith(('content','Website.','Includehelp'),24,51))

#返回False

print('When tuple is passed without/with optional parameters:',string1.endswith(('Technical' , 'Content', 'Website.'),24,51))

输出结果

endswith() Without start and end parameters

When no optional parameter is passed: True

When no optional parameter is passed: False

When large string is to be found: True

endswith() With start and end Parameters

When optional parameter is passed: True

When optional parameter is passed: False

When large string is to be found with optional parameters: False

endswith() With Tuple Suffix

When tuple is passed without optional parameters: True

When tuple is passed with optional parameters: True

When tuple is passed without/with optional parameters: True

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值