《Python編程 從入門到實踐》-基礎知識補充

1.請在終端輸出(包括最外層雙引號):

"The language 'Python' is named afterMonty Python, not the snake."

代碼:print ('"'"The language 'Python'is named after Monty Python, not the snake."'"')

或者:print ('"'+"Thelanguage 'Python' is named after Monty Python, not the snake."+'"')

注意:輸出雙引號用單引號括起來輸出   +爲連接符


2.字符串“ don not give up. Never give up until the flight is over!”尾部感嘆號

代碼:msg=" don not give up. Never give up until the flight is over!"    

          print (msg[:-1])

或者:print(msg.rstrip("!"))  

注意:print (msg[:-1]) 這句話沒有刪除最後一個字符的意思,是截取(術語叫做slice,切片),是指將字符串的開頭起截取到字符串末尾倒數第二個字符,並輸出,所以感嘆號沒有了

          這裏,msg的值不僅是一個字符串,它是一個 String 對象,包含 rstrip() 的去尾方法,括號裏是可以寫上其他參數的,可以刪除任意符號

3.插入三個字符串(append,insert,extend)

A.extend(["wang","lin","jiao"])

臨時逆序排序(sort/sorted可以寫多個參數)

print(sorted(A,reverse=True))

插入字符串(insert)

A.insert(0,'5')

刪除值(pop(2),2爲索引值 remove移除字符)

A.pop(2)

枚舉(enumerate(p1,p2))參數p1指列表,p2代表設置索引值起始位置,默認爲0

for index,item in enumerate(A,1):
	print(str(index)+'\t'+item)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章