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