Python判断字符串是否是中英文小技巧总结
更新时间:2023年06月29日 09:53:28 作者:袁袁袁袁满
这篇文章主要给大家介绍了关于Python判断字符串是否是中英文小技巧的相关资料,这个在实际应用中十分常见,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
前言
博主工作中刚好用到了Python检测中英文文的小技巧,记录一下
一、检验是否全是中文字符
def is_all_chinese(strs):
for _char in strs:
if not '\u4e00' <= _char <= '\u9fa5':
return False
return True
print(is_all_chinese("hello"))
print(is_all_chinese("hello你好"))
print(is_all_chinese("123456"))
print(is_all_chinese("你好"))输出结果:
False
False
False
True
二、检验是否含有中文字符
def is_contains_chinese(strs):
for _char in strs:
if '\u4e00' <= _char <= '\u9fa5':
return True
return False
print(is_contains_chinese("hello"))
print(is_contains_chinese("hello你好"))
print(is_contains_chinese("123456"))
print(is_contains_chinese("你好"))输出结果:
False
True
False
True
三、检测是否全是英文字符
def is_all_english(strs):
import string
for i in strs:
if i not in string.ascii_lowercase + string.ascii_uppercase:
return False
return True
print(is_all_english("hello"))
print(is_all_english("hello你好"))
print(is_all_english("123456"))
print(is_all_english("你好"))输出结果:
True
False
False
False
四、检测是否含有英文字符
import re
def is_contains_english(str):
my_re = re.compile(r'[A-Za-z]', re.S)
res = re.findall(my_re, str)
if len(res):
return True
else:
return False
print(is_contains_english("hello"))
print(is_contains_english("hello你好"))
print(is_contains_english("123456"))
print(is_contains_english("你好"))
输出结果:
True
True
False
False
总结
到此这篇关于Python判断字符串是否是中英文小技巧的文章就介绍到这了,更多相关Python判断字符串是中英文内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Python threading和Thread模块及线程的实现
这篇文章主要介绍了Python threading和Thread模块及线程的实现,Python通过两个标准库thread和threading提供对线程的支持,threading对thread进行了封装,具体实现介绍需要的朋友可以参考一下下面文章内容2022-06-06
Python 在 VSCode 中使用 IPython Kernel 的方法详解
这篇文章主要介绍了Python 在 VSCode 中使用 IPython Kernel 的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-09-09


最新评论