圖解+筆記-4.2.1 字串獲取操作、字串合併和重複操作
字串獲取操作
概念:用雙引號或者單引號括起來的一串字元
字串按下標獲取操作
定義字串
>>> S="abcd"
給字串求長度
>>> len(S)
4
從正數數字從左邊開始獲取單個字元:從0開始
>>> S[0]
'a'
>>> S[2]
'c'
>>> S[20] #這裡越界了,對“abcd”最大是3
Traceback (most recent call last):
File "<pyshell#37>", line 1, in <module>
S[20]
IndexError: string index out of range
>>>
從正數數字從右邊開始獲取單個字元:從-1開始
>> S[-1]
'd'
>>> S[-2]
'c'
>>> S[-5]
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
S[-5]
IndexError: string index out of range
雙邊正數範圍擷取子串
>>> S[0:100] 最新從0開始,最大無限制,但是超過字串長度則只返回最後的字元
'abcd'
>>> S[1:3] 不包括最右邊的3對應的字元
'bc'
>>>
單邊正數範圍擷取子串
>>> S[1:] 從1開始到最後
'bcd'
>>> S[:3] 從0開始到3
'abc'
>>>
雙邊負數範圍擷取子串
>>> S[-3:-1] 不包括-1對應的字元
'bc'
單邊負數範圍擷取子串
>>> S[-3:] 從-3開始到最後
'bcd'
>>> S[:-2] 從最左邊開始到倒數第二
'ab'
字串合併和重複操作
合併操作
>>> S + "xyz"
'abcdxyz'
重複操作
>>> S * 3
'abcdabcdabcd'
* 打折課程大放送
1. 【圖解+筆記】Python從入門到進階大合集詳細講解(含500條筆記):http://edu.51cto.com/sd/99cf7
2. 【筆記式】Python基礎入門--八大資料結構篇(含200條筆記):http://edu.51cto.com/sd/f967d
3. 【筆記式】Python基礎入門--語句函式篇(含150條筆記):http://edu.51cto.com/sd/eea85
4. 【圖解+筆記】Python進階-模組、包、面向物件篇(含200條筆記):http://edu.51cto.com/sd/43049
5. 【筆記式】Maven從入門到進階大合集詳細講解(含200條筆記):http://edu.51cto.com/sd/09299
6. 【筆記式】Maven基礎入門--座標倉庫私服(含100條筆記):http://edu.51cto.com/sd/e1387
7. 【筆記式】Maven高階進階--外掛測試屬性配置(含100條筆記):http://edu.51cto.com/sd/f3fda