Python判断字符串中是否有中文小技巧

简介: 博主工作中刚好用到了检测中文的小技巧,记录一下

目录

前言

检验是否全是中文字符

检验是否含有中文字符

前言

博主工作中刚好用到了检测中文的小技巧,记录一下


检验是否全是中文字符

def is_all_chinese(strs):
    for _char in strs:
        if not '\u4e00' <= _char <= '\u9fa5':
            return False
    return True
# 案例
print(is_all_chinese('你好,hello')) # False
print(is_all_chinese('你好')) # True

检验是否含有中文字符

def is_contains_chinese(strs):
    for _char in strs:
        if '\u4e00' <= _char <= '\u9fa5':
            return True
    return False
# 案例
print(is_contains_chinese('你好,hello')) # True
print(is_contains_chinese('你好')) # True
相关文章
|
9月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
477 100
|
9月前
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
627 99
|
9月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
9月前
|
开发者 Python
Python f-strings:更优雅的字符串格式化技巧
Python f-strings:更优雅的字符串格式化技巧
|
9月前
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
9月前
|
Python
使用Python f-strings实现更优雅的字符串格式化
使用Python f-strings实现更优雅的字符串格式化
|
10月前
|
索引 Python
python 字符串的所有基础知识
python 字符串的所有基础知识
481 0
|
10月前
|
Python
Python字符串center()方法详解 - 实现字符串居中对齐的完整指南
Python的`center()`方法用于将字符串居中,并通过指定宽度和填充字符美化输出格式,常用于文本对齐、标题及表格设计。
|
10月前
|
Python
Python中的f-string:更简洁的字符串格式化
Python中的f-string:更简洁的字符串格式化
441 92

推荐镜像

更多