(十三)創建一個你最喜歡歌手的列表。
1 # singer=list()
2 # singer=['a','b','c']
3 # print(singer)
(十四)創建一個由元組構成的列表,每個元組包含居住過或旅游過的城市的經緯度。
1 # s=tuple('1.1','2.2','3.3')
2 # print(s)
(十五)創建一個包含你的不同屬性的字典:身高、最喜歡的顏色和最喜歡的作者等。
1 # zi_dian={"height":"1.9m",
2 # "color":"blue",
3 # "author":"魯迅"}
4 # print(zi_dian)
(十六)編寫一個程序,讓用戶詢問你的身高、最喜歡的顏色或最喜歡的作者,並返回上一個挑戰中創建的字典。
1 # zi_dian={"height":"1.9m",
2 # "color":"blue",
3 # "author":"魯迅"}
4 # print(zi_dian["height"])
5 # print(zi_dian["color"])
6 # print(zi_dian["author"])
7 # print(zi_dian)
(十七)創建一個字典,將最喜歡的歌手映射至你最喜歡的歌曲。
1 # singer={"123":"456",
2 # "789":"998"}
3 # print("singer")
(十八)列表、元組和容器只是Python 中內置容器的一部分。自行研究Python 中的集合.(也是一種容器)在什么情況下可以使用集合?
:思考一下,集合與list,tuple的區別
(十九)打印字符串"Camus"中的所有字符。
1 # v="Camus"
2 # print(v[0])
3 # print(v[1])
4 # print(v[2])
5 # print(v[3])
6 # print(v[4])
(二十)編寫程序,從用戶處獲取兩個字符串,將其插入字符串"Yesterday I wrote a[用戶輸入1]. I sent it to [用戶輸入2]!"中,並打印新字符串。
1 # c=input("type a str:")
2 # d=input("type a str:")
3 # a="Yesterday I wrote a {}. I sent it to {}!".format(c,d)
4 # print(a)
(二十一)想辦法將字符串"aldous Huxley was born in 1894."的第一個字符大寫,從而使語法正確。
1 # a="aldous Huxley was born in 1894"
2 # v=a.capitalize()
3 # print(v)
(二十二)對字符串"Where now? Who now? When now?"調用一個方法,返回如下述的列表["Where now", "Who now", "When now"]。
1 # a="Where now? Who now? When now? "
2 # v=a.split("?")
3 # print(v)
(二十三)對列表["The", "fox", "jumped", "over", "the", "fence", "."]進行處理,將其變成一個語法正確的字符串。每個單詞間以空格符分隔,但是單詞fence 和句號之間不能有空格符。(別忘了,我們之前已經學過將字符串列表連接為單個字符串的方法。)
1 # s=["The", "fox", "jumped", "over", "the", "fence", "."]
2 # v=" ".join(s)
3 # v=v.strip()
4 # print(v)
這是錯誤的方法!
1 #第一種方法
2 fox = ["The", "fox", "jumped", "over", "the", "fence", "."]
3 fox = " ".join(fox)
4 fox = fox[0: -2] + "."
5 print(fox)
6
7 #第二種方法
8 lists = ["The", "fox", "jumped", "over", "the", "fence", "."]
9 f = ' '.join(lists).title()
10 x = f.replace(' .','.')
11 print(x)
12
13 #在此感謝https://home.cnblogs.com/u/1805839/的指正
如有錯誤,歡迎指正!