您现在的位置是:课程教程文章

python 怎样计算字符串的长度

2023-12-18 22:06课程教程文章 人已围观

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

python学习网,大量的免费python视频教程,欢迎在线学习!

相关推荐:《Python教程》

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

结果输出:

6
课程教程:python 怎样计算字符串的长度

上一篇:python中魔法怎么使用

下一篇:没有了

站点信息

  • 文章统计篇文章