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

python contains Chinese string length

大家讲道理
Release: 2016-11-09 11:14:17
Original
2236 people have browsed it

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
Copy after login
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template