# 示例代码:使用 Python 的 endswith 函数
# 定义一个字符串
text = "hello world"
# 检查字符串是否以 "world" 结尾
if text.endswith("world"):
print("字符串以 'world' 结尾")
else:
print("字符串不以 'world' 结尾")
# 检查字符串是否以 "python" 结尾
if text.endswith("python"):
print("字符串以 'python' 结尾")
else:
print("字符串不以 'python' 结尾")
# 你还可以传递一个元组,检查多个可能的结尾
suffixes = ("world", "python")
if text.endswith(suffixes):
print(f"字符串以 {suffixes} 中的一个结尾")
else:
print(f"字符串不以 {suffixes} 中的任何一个结尾")
endswith
是 Python 字符串的一个方法,用于检查字符串是否以指定的子字符串结尾。True
,否则返回 False
。上一篇:python获取路径的最后一级
下一篇:python中切片
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站