Python中遍历字符串和字典

遍历字符串

>>> for x in  "ILoveYou":
 print(x)


I
L
o
v
e
Y
o
u

遍历字典

遍历所有key两种方式

>>> d = {'name':'badao','age':'24','adress':'中国'}
>>> for x in d:
 print(x)


name
age
adress
>>> for x in d.keys():
 print(x)


name
age
adress

遍历所有values

>>> for x in d.values():
 print(x)


badao
24
中国

遍历所有键值对

>>> for x in d.items():
 print(x)


('name', 'badao')
('age', '24')
('adress', '中国')
>>>

猜你喜欢

转载自blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/84568498