Home >Backend Development >Python Tutorial >python contains Chinese string length

python contains Chinese string length

大家讲道理
大家讲道理Original
2016-11-09 11:14:172248browse

Python contains the length of Chinese strings

A Chinese utf8 encoding occupies 3 characters, so the function to find the length can be written like this

def str_len(str):
    try:
        row_l=len(str)
        utf8_l=len(str.encode('utf-8'))
        return (utf8_l-row_l)/2+row_l
    except:
        return None
    return None
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn