python學習之路5 字典 字串

2021-08-22 12:15:09 字數 1235 閱讀 8565

字典是python中的唯一對映型別,採用鍵值對來儲存。

特點:無序

1.字典的建立:

dict = 

print(dict)

2.四種方法-增刪改查

#增

dict['qq']='1234'#直接賦值即可,若有會覆蓋。

dict.setdefault('tt',88)

dict =

dict1 =

dict.update(dict1) #新增字典,有重複會覆蓋

print(dict)

#刪# dict =

# del dict['hi']#刪除某個鍵值對

# print(dict)

# dict =

# ret=dict.pop('ll')#刪除鍵值對,並有返回值

# print(dict)

# print(ret)

# dict =

# dict.popitem()#隨機刪除

# print(dict)

#字典的遍歷

# dict1 =

# for i in dict1:

# print(i,dict1[i])

string

a='123'

b='145'

c=''.join([a,b])#合併兩個字串

print(c)

#一些重要的字串內建方法

#重要字串方法:

# print(str.count('l')) #次數

# print(str.center(50,'*'))

# print(str.startswith('h'))#以什麼開始

# print(str.find("o"))#尋找元素並返回索引值

# print(str.format(name = 'lee'))#格式化輸出

# print("mgksm".lower())

# print("fsfs".upper())

# print("\tooo\n".strip())#去掉換行服

# print("my book".replace("my","you"))

# print("my title title".split("i")) #以i為分割物件 成為列表['my t', 'tle t', 'tle']

python基礎學習5 字典型別

定義 逗號分隔多個鍵值對,a b a是鍵 通常是字串 b是值 可以是任意型別 xiaoming 訪問 使用 字典名 鍵名 訪問元素 ps 不可按列表的順序訪問,因為字典型別不關心順序 xiaoming age result 20 新增鍵值對 直接定義並賦值 xiaoming girlfrd xiao...

python學習之路五 字典 dict

python中的字典 1.乙個簡單的字典 person 2.訪問字典中的資料 print person name 3.新增鍵 值對 字典是一種動態結構,可隨時在其中新增鍵 值對 person height 180 4.修改字典中的值 person height 175 5.刪除鍵 值對 使用del語...

Python 5 字典,集合

字典建立 a 用內建函式dict 快速建立字典 keys a b c d values 1,2,3,4 adict dict zip x,y print adict adict dict name wang age 22 adict 字典元素增加,修改 keys name age values hu...