1、Str 的常用操作
1.1字符串的拼接
>>> a = 'hello'
>>> b = 'python'
>>> a + '-' + b
'hello-python'
>>> a + b
'hellopython'
但是字符串和非字符串不能拼接
要想拼接得使用字符串的类型转换 str()
>>> a + str(123)
'hello123'
1.2 长度计算 len()
>>> len('我要好好学习python')
12
1.3 截取
字符串是一种特殊的序列,对于列表和元组对它同样适用
>>> a = 'hello'
>>> b = 'python'
>>> a[1]
'e'
>>> 'aaabbbccc'[8]
'c'
>>> b[-2]
'o'
>>> a[1:2]
'e'
>>> a[1:3]
'el'
>>> a[:4]
'hell'
1.4 分隔 split()
用空格和逗号
>>> '1 2 3 4 5'.split()
['1', '2', '3', '4', '5']
>>> '1 2 3 4 5'.split()
['1', '2', '3', '4', '5']