Python中可以使用字符串的endswith()方法来检查字符串中的结束标记。

语法:

str.endswith(suffix[, start[, end]])

参数说明:

  • suffix:必需,指定要检查的字符串或字符。
  • start:可选,表示起始位置,默认值为0,即从字符串的起始位置开始检查。
  • end:可选,表示结束位置,默认值为字符串的长度,即一直检查到字符串末尾。

示例:

str1 = "Hello, world!"
str2 = "Python is cool."
str3 = "I love Python."

# 检查字符串是否以指定的后缀结尾
if str1.endswith("!"):
    print("字符串str1以感叹号结尾")
else:
    print("字符串str1不以感叹号结尾")

if str2.endswith("."):
    print("字符串str2以句号结尾")
else:
    print("字符串str2不以句号结尾")

if str3.endswith("Python"):
    print("字符串str3以Python结尾")
else:
    print("字符串str3不以Python结尾")

输出:

字符串str1以感叹号结尾
字符串str2以句号结尾
字符串str3以Python结尾
Python检查字符串中的结束标记

原文地址: https://www.cveoy.top/t/topic/56v 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录