python计算字符串长度(中文算2个字符)

版权声明:本文为博主原创文章,转载请表明来源和网址。 https://blog.csdn.net/thinbug/article/details/85167202
    txt = '名字12' 
    lenTxt = len(txt) 
    lenTxt_utf8 = len(txt.encode('utf-8')) 
    size = int((lenTxt_utf8 - lenTxt)/2 + lenTxt)

    print("size = " , size ," ,urf8 = ",lenTxt_utf8," ,len = " ,lenTxt)

猜你喜欢

转载自blog.csdn.net/thinbug/article/details/85167202
今日推荐