可以使用字符串的 count() 方法来统计字符串中 * 的个数,然后判断是否等于 1。

示例代码:

def has_one_star(s):
    return s.count('*') == 1

# 测试
print(has_one_star('abc*def'))  # True
print(has_one_star('abc*def*'))  # False
print(has_one_star('*abc*def'))  # False

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

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