1. 字符串拼接
>>> 'hello' + 'world'
'helloworld'
>>> 'hello' * 3
'hellohellohello'
>>>
2. 获取字符串中某个字符
>>> 'hello world'[0]
'h'
>>> 'hello world'[1]
'e'
>>> 'hello world'[2
... ]
'l'
>>> 'hello world'[3]
'l'
>>> 'hello world'[4]
'o'
>>> 'hello world'[5]
' '
获取某个字符,直接输入此字符在字符串中的下标,切记,下标从0开始
>>> 'hello world'[-1]
'd'
注意,str[-n],-n为负数的话,从后面数n个字符
- 字符切片
>>> 'Hello World'[6:11]
'World'
>>> 'Hello World'[6:]
'World'
>>> 'Hello Wolrd'[6:-1]
'Wolr'
>>>
使用[初始位置下标:需要切片的到元素的下个位置]
网友评论