📜  Python字符串 endwith() 方法

📅  最后修改于: 2022-05-13 01:55:43.381000             🧑  作者: Mango

Python字符串 endwith() 方法

如果字符串以给定的后缀结尾, Python String endswith() 方法返回 True,否则返回 False。

示例 1:没有 start 和 end 参数的 endswith() 方法的工作

Python
# Python code shows the working of
# .endswith() function
  
text = "geeks for geeks."
  
# returns False
result = text.endswith('for geeks')
print (result)
  
# returns True
result = text.endswith('geeks.')
print (result)
  
# returns True
result = text.endswith('for geeks.')
print (result)
  
# returns True
result = text.endswith('geeks for geeks.')
print (result)


Python
# Python code shows the working of
# .endswith() function
  
text = "geeks for geeks."
  
# start parameter: 10
result = text.endswith('geeks.', 10)
print(result)
  
# Both start and end is provided
# start: 10, end: 16 - 1
# Returns False
result = text.endswith('geeks', 10, 16)
print result
  
# returns True
result = text.endswith('geeks', 10, 15)
print result


输出:

False
True
True
True

示例 2:带有 start 和 end 参数的 endswith() 方法的工作

Python

# Python code shows the working of
# .endswith() function
  
text = "geeks for geeks."
  
# start parameter: 10
result = text.endswith('geeks.', 10)
print(result)
  
# Both start and end is provided
# start: 10, end: 16 - 1
# Returns False
result = text.endswith('geeks', 10, 16)
print result
  
# returns True
result = text.endswith('geeks', 10, 15)
print result

输出:

True
True
False