• 技术文章 >常见问题 >Python常见问题

    python判断字符串是否包含中文

    (*-*)浩(*-*)浩2019-08-27 11:13:43原创12624

    原理:

    中文字符的编码范围是:

    1

    \u4e00 - \u9fff

    只要编码在此范围就可判断为中文字符

    以下代码Python2下测试有效

    判断字符串中是否包含中文

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    def is_contain_chinese(check_str):

        """

        判断字符串中是否包含中文

        :param check_str: {str} 需要检测的字符串

        :return: {bool} 包含返回True, 不包含返回False

        """

        for ch in check_str:

            if u'\u4e00' <= ch <= u'\u9fff':

                return True

        return False

    整个字符串都是中文

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    def is_chinese(string):

        """

        检查整个字符串是否为中文

        Args:

            string (str): 需要检查的字符串,包含空格也是False

        Return

            bool

        """

        for chart in string:

            if chart < u'\u4e00' or chart > u'\u9fff':

                return False

     

        return True

    专题推荐:python
    上一篇:Python判断一个字符串是否包含另一个字符串 下一篇:python pep是什么

    相关文章推荐

    • python文档怎么看• python函数如何不返回none• python模块放在哪• python的range函数怎么用

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网