首页 > 编程语言 > 详细

[转载] python 计算字符串长度

时间:2018-02-07 11:07:14      阅读:198      评论:0      收藏:0      [点我收藏+]
本文转载自: http://www.sharejs.com/codes/python/4843

 

python 计算字符串长度,一个中文算两个字符,先转换成utf8,然后通过计算utf8的长度和len函数取得的长度,进行对比即可知道字符串内中文字符的数量,自然就可以计算出字符串的长度了。

value=u‘脚本12‘
length = len(value)
utf8_length = len(value.encode(‘utf-8‘))
length = (utf8_length - length)/2 + length
print(length)

结果输出6

[转载] python 计算字符串长度

原文:https://www.cnblogs.com/the-tops/p/8425517.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!