str:(字符串)
-
首字母大寫: capitalize() -- s1 = s.capitalize()
-
每個單詞首字母大寫: title -- s1 = s.title()
-
統計出現的次數 : count -- s.count("+")
-
大小寫轉換; s.swapcase()
-
查找 :
- find -- s.find("+") -- 查找不到時返回-1
- index -- s.index("+") -- 找不到就報錯
-
找下標:
for i in range(len(s)):
if s[i] == "+"
print(i)
list:(列表)
- 統計 : count -- li.count("+")
- 查看 : index -- li.index("+")
- 反轉 : reverse -- li.reverse()
- 排序 :
- sort -- li.sort() 升序
- sort(reverse = True) 降序
tuple:(元組)
- tu = tuple("123456") 字符串轉化成元組:
- 統計 : count -- tu.count("3")
- 查找 : index -- tu.index("1")
dict:(字典)
- dic = dict(k=1,k2=2,k3=3,k4=4,k5=5,k6=6) 定義方式;
- 隨機刪除 popitem - - dic.popitem()
- python3.6版本默認刪除最后一個鍵值對
- python3.5版本以前 隨機刪除
- 批量創建字典 : formkeys -- dict.fromkeys("sdadadad",[4,5,4])
- 第一個參數是可迭代對象
- 第二個參數是每個建對應的值,用的都是同一個內存地址.
set : (集合)
s = set("235645")定義方式:
數據類型轉換:
- str -- int 字符串中必須都是十進制的數才可以轉換 直接用int轉換就行;
- int -- str 沒有特殊要求 直接用str轉換就行;
- str -- list 無特殊要求直接用list轉換就行; split -- s.split() 將字符串當成一個整體轉換為列表;
- list -- str 直接用str轉換就行,當用 join 時 列表內不能有數字 ("".join(li))
- list -- tuple 直接用tuple轉化即可
- tuple -- list 直接用list轉化就行
- list -- set 直接用set轉化就行
str - int - bool - list - tuple - dict - set :
有序 | 無序 | 可變 | 不可變 | 直接訪問 | 順序訪問 | 通過鍵訪問 | |
---|---|---|---|---|---|---|---|
str(字符串) | 1 | 1 | 1 | ||||
int(整型) | 1 | 1 | 1 | ||||
bool(布爾值) | 1 | 1 | 1 | ||||
list(列表) | 1 | 1 | 1 | ||||
tuple(元組) | 1 | 1 | 1 | ||||
dict(字典) | 1 | 1 | 1 | ||||
set(集合) | 1 | 1 | 1 |
常見的易錯點:
- 使用for刪除列表的時候要從右往左刪除不然會報錯
- 不能在遍歷字典本身中改變大小,要批量刪除字典的鍵值對
- 循環刪除索引的時候,要倒序刪除否則會報錯或結果不正確;
- 循環添加元素的時候,會形成死循環;
- 字典在循環的時候不能改變字典的大小
- 先定義一個列表,循環列表,列表刪除字典
編碼:
- 硬盤上儲存的是字節;
- python3內使用的unicode
- python2中使用的是ascii
- 用什么編碼用什么解碼
- encode() 編碼
- decode() 解碼