网络编程
位置:首页>> 网络编程>> Python编程>> python 检查是否为中文字符串的方法

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

作者:August1226  发布时间:2023-11-22 17:29:57 

标签: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 检查是否为中文字符串的方法

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

来源:https://blog.csdn.net/August1226/article/details/80626396

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com