python中文utf8编码后是占3个字符,unicode汉字为2字节

一个中文utf8编码后是占3个字符,所以求长度的函数可以这样写
        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  
unicode中汉字为两字节, utf-8中汉字为三字节

 https://en.wikipedia.org/wiki/Unicode

https://en.wikipedia.org/wiki/UTF-8

原文地址:https://www.cnblogs.com/timssd/p/4735196.html