当前位置:主页 > 软件编程 > Python代码 >

python 检查是否为中文字符串的方法

时间:2021-03-19 09:44:54 | 栏目:Python代码 | 点击:

【目标需求】

查看某一个字符串是否为中文字符串

解决办法

def check_contain_chinese(check_str):
 for ch in check_str:
 if u'\u4e00' <= ch <= u'\u9fff':
  return True 
 else:
  return False

【举例检验】

check_contain_chinese('abcc')
False
check_contain_chinese('123')
False
check_contain_chinese('中文')
True

问题解决!

-----------------2018-07-27 更新-----------------

【更新】

上面的脚本实际上只识别了字符串的第一个字符,下面的版本则可以用来识别字符串中是否【包含or全是】中文字符

#检验是否含有中文字符
def isContainChinese(s):
 for c in s:
 if ('\u4e00' <= c <= '\u9fa5'):
  return True
 return False
 
#检验是否全是中文字符
def isAllChinese(s):
 for c in s:
 if not('\u4e00' <= c <= '\u9fa5'):
  return False
 return True

检验结果展示:

python 检查是否为中文字符串

(仅供个人学习,不负责任,嘻嘻~~)

您可能感兴趣的文章:

相关文章