endswith方法
Python字符串endswith()方法 (Python String endswith() Method)
endswith() Method is a library method in Python, it is used to check whether a string ends with a given suffix (substring) or not. It returns True – if the string ends with given suffix else if returns False.
endwith()方法是Python中的一种库方法,用于检查字符串是否以给定的后缀(子字符串)结尾。 它返回True –如果字符串以给定的后缀结尾,否则返回False 。
Syntax:
句法:
String.endswith(suffix, start, end)
Parameter(s):
参数:
suffix: This may a substring or the tuple that we are looking in the string.
后缀 :这可能是子字符串或我们在字符串中查找的元组。
start: It is an optional parameter in the endswith() method and it's default value is 0. It is the starting point in the string from where we want to start checking for the suffix.
start :这是endswith()方法中的一个可选参数,默认值为0。它是字符串中我们要开始检查后缀的起点。
end: It is also an optional parameter in the endswith() method and it's default value is -1. It defines the ending point in the string from where the suffix is needed within i.e. till where we want to find the suffix.
end :也是endswith()方法中的一个可选参数,默认值为-1。 它在字符串中定义了需要后缀的字符串的终点,即直到我们要找到后缀的位置。
Return value:
返回值:
It returns True if string ends with the given suffix otherwise returns false.
如果字符串以给定的后缀结尾,则返回True,否则返回false。
Python代码演示String.endswith()方法的示例 (Python code to demonstrate example of String.endswith() method)
# endswith() w/o start and end Parameters
print('endswith() Without start and end parameters')
string1='IncludeHelp is the Best Technical Content Website.'
suffix='Website.'
#returns True
print('When no optional parameter is passed:',string1.endswith(suffix))
#retuns False as the '.' is not inc;luded
print('When no optional parameter is passed:',string1.endswith('Website'))
#more than one word may be there but a single string is there.
print('When large string is to be found:',string1.endswith('Technical Content Website.'))
print()
print()
print()
# endswith() With start and end Parameters
print('endswith() With start and end Parameters')
# Both start and end is provided
# start: 42, end: 51
# "programming is easy" string is searched
#returns True
print('When optional parameter is passed:',string1.endswith(suffix,42,51))
#retuns False as the '.' is not inc;luded
print('When optional parameter is passed:',string1.endswith('Website',43,52))
#more than one word may be there but a single string is there.
print('When large string is to be found with optional parameters:',string1.endswith('Technical Content Website.',42,51))
print()
print()
print()
#endswith() With Tuple Suffix
print('endswith() With Tuple Suffix')
#returns True
print('When tuple is passed without optional parameters:',string1.endswith(('content','Website.','Includehelp')))
#retuns True
print('When tuple is passed with optional parameters:',string1.endswith(('content','Website.','Includehelp'),24,51))
#returns False
print('When tuple is passed without/with optional parameters:',string1.endswith(('Technical' , 'Content', 'Website.'),24,51))
Output
输出量
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
翻译自: https://www.includehelp.com/python/string-endswith-method-with-example.aspx
endswith方法