最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python判斷字符串是否是中英文小技巧總結(jié)

 更新時間:2023年06月29日 09:53:28   作者:袁袁袁袁滿  
這篇文章主要給大家介紹了關(guān)于Python判斷字符串是否是中英文小技巧的相關(guān)資料,這個在實際應用中十分常見,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

前言

博主工作中剛好用到了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("你好"))

輸出結(jié)果:

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("你好"))

輸出結(jié)果:

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("你好"))

輸出結(jié)果:

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("你好"))

輸出結(jié)果:

True
True
False
False

總結(jié)

到此這篇關(guān)于Python判斷字符串是否是中英文小技巧的文章就介紹到這了,更多相關(guān)Python判斷字符串是中英文內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

星子县| 邳州市| 巢湖市| 宁阳县| 嘉善县| 泸州市| 平舆县| 彰化县| 新郑市| 濉溪县| 西藏| 彭阳县| 塘沽区| 阳东县| 洛隆县| 互助| 普洱| 揭阳市| 兰坪| 南昌县| 遂溪县| 汝南县| 竹山县| 墨玉县| 怀宁县| 岳普湖县| 广州市| 酉阳| 施甸县| 霍城县| 凉山| 微博| 柳江县| 喀喇| 五寨县| 武宁县| 和田县| 松滋市| 六盘水市| 阿鲁科尔沁旗| 桐乡市|