【目标需求】
【解决办法】
1
2
3
4
5
6
|
def check_contain_chinese(check_str):
for ch in check_str:
if u '\u4e00' < = ch < = u '\u9fff' :
return True
else :
return False
|
【举例检验】
1
2
3
4
5
6
|
check_contain_chinese( 'abcc' )
False
check_contain_chinese( '123' )
False
check_contain_chinese( '中文' )
True
|
问题解决!
-----------------2018-07-27 更新-----------------
【更新】
上面的脚本实际上只识别了字符串的第一个字符,下面的版本则可以用来识别字符串中是否【包含or全是】中文字符
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#检验是否含有中文字符
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 检查是否为中文字符串的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/August1226/article/details/80626396