(二十四)將字符串"A screaming comes across the sky."中所有的"s"字符替換為美元符號。
1 # a="A screaming comes across the sky." 2 # a=a.replace("s","$") 3 # print(a)
(二十五)找到字符串"Hemingway"中字符"m"所在的第一個索引。
1 # a="Hemingway" 2 # print(a.index("m"))
(二十六)在你最喜歡的書中找一段對話,將其變成一個字符串。
1 # a="歲月不待人" 2 # print(a)
(二十七)先后使用字符串拼接和字符串乘法,創建字符串"three three three"。
1 # a="thr" 2 # b="ee" 3 # c=a+b 4 # print(c) 5 # v=c*3 6 # print(v) 輸出“threethreethree”
#補充:
# v=c+" "
# print(v*3) 輸出“three three three ”
(二十八)對字符串"It was bright cold day in April, and the clocks were striking thirteen."進行切片,只保留逗號之前的字符。
1 # a="It was bright cold day in April,and the clorcks were strking thirteen." 2 # a=a.split(",") 3 # # print(a) 4 # print(a[0:1])
(二十九)打印以下列表["The Walking Dead", "Entourage", "The Sopranos","The Vampire Diaries"]中的每個元素。
1 # q=["The Walking Dead", "Entourage", "The Sopranos","The Vampire Diaries"] 2 # for i in q: 3 # print(i)
(三十)打印從25 到50 之間的所有數字。
1 # for i in range(25,50): 2 # print(i)
(三十一)打印第一個挑戰練習中的每個元素及其索引。
1 # a=["The Walking Dead", "Entourage", "The Sopranos","The Vampire Diaries"] 2 # for i in a : 3 # print(i) 4 # s=a.index(i) 5 # print(s)
(三十二)編寫一個包含死循環和數字列表的程序(可選擇輸入q 退出)。每次循環時,請用戶猜一個在列表中的數字,然后告知其猜測是否正確。
1 # a=input("type a str:") 2 # while a=="b": 3 # if a=="q": 4 # break 5 # end 6 # print("right!")
(三十三)將列表[8, 19, 148, 4]中的所有數字,與列表[9, 1, 33, 83]中的所有數字相乘,並將結果添加到第3 個列表中。
1 # a=[8,19,148,4] 2 # s=[9,1,33,83] 3 # d=[] 4 # for i in a: 5 # for j in s: 6 # d.append(i*j) 7 # print(d)
如有不足,歡迎指正!
