推荐
关注
TOP
Message

判断 字符串是否包含某个字符

1. 可以自己写个方法来判断

def IsContains(strings, text)->bool:
    """
    :param strings:  被包含的值
    :param text:     原值
    :return: 返回的是一个布尔值
    """
    try:
        if isinstance(strings, str):
            if strings.find(text):
                return True
            else:
                return False
        else:
            return False
    except Exception as e:
        print(f"意外中断……{e}")


if __name__ == "__main__":
    print(IsContains("傻逼", "你是大傻逼"))  # True

2. 直接调用operator的contains方法

用法如下

import operator

# True
print(operator.contains("hello,world", "hello"))

3. 使用关键字 in

用法如下

print("t" in "test") # true
posted @ 2022-03-23 13:13  始識  阅读(226)  评论(0编辑  收藏  举报