我剛剛開始學習 Python,我剛剛開始學習字串。所以我這里有 3 個命令
_teststr = "IneedToFocusOnMyFuture"
print('1.',_teststr[::-1])
print('2.',_teststr[2:12:2])
print('3.',_teststr[2:12:-1])
輸出:
1. erutuFyMnOsucoFoTdeenI
2. edoou
3.
為什么第三個輸出是空白而不是有'uoode'?引擎蓋下發生了什么?有人可以解釋一下嗎?
uj5u.com熱心網友回復:
如果要反轉特定字串。
print('3.',_teststr[2:12][::-1])
讓我們了解語法
str[start:end:step]
start 表示從哪里拼接,end 表示從哪里結束拼接。step引數表示拼接成功后要離開的元素個數,默認step為1。
例子:
str="naveen"
str[1:3] //av
#with including step
str[1:3:1] //av
str[1:4:2] //ae
step defines the number of elements to leave after a consideration.So it cant be negative.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/336993.html
上一篇:pandasloc檢查值是否包含多個單詞或字串中的任何一個
下一篇:需要幫助進行p語言解密
