字符串长度

可使用len()函数计算字符串长度,基本语法格式为:

len(string)

举例:定义字符串'i want to go to bed',计算其长度

a='i want to go to bed'
print(len(a))
>>>19

字符串字节数

>>> str1 = "人生苦短,我用Python"
>>> len(str1.encode())
27