Python3 检查字符串是否包含中文

  • 本版本为Python3 的版本
  • 导入依赖包
import importlib, sys
importlib.reload(sys)
  • 定义判断函数
def check_contain_chinese(check_str):
    for ch in check_str.encode('utf-8').decode('utf-8'):
        if u'\u4e00' <= ch <= u'u9fff':
            return True
    return False
  • 调用函数
check_contain_chinese('I love you')
  • 返回结果
False

参考文章:

  1. https://www.cnblogs.com/changzhi/p/3376140.html
  2. 汉字Unicode区间
  3. Python代码实现

Guess you like

Origin blog.csdn.net/weixin_42279212/article/details/120509719