python 计算字符串长度,一个中文算两个字符,先转换成utf8,而后经过计算utf8的长度和len函数取得的长度,进行对比便可知道字符串内中文字符的数量,天然就能够计算出字符串的长度了。python
value=u'脚本12' length = len(value) utf8_length = len(value.encode('utf-8')) length = (utf8_length - length)/2 + length print(length)
结果输出6函数
python 计算字符串长度,一个中文算两个字符,先转换成utf8,而后经过计算utf8的长度和len函数取得的长度,进行对比便可知道字符串内中文字符的数量,天然就能够计算出字符串的长度了。python
value=u'脚本12' length = len(value) utf8_length = len(value.encode('utf-8')) length = (utf8_length - length)/2 + length print(length)
结果输出6函数