2013-10-09 78 views
-1

任何人能解釋我:s[81:67:-1]

s是「this is me asking a question on stackoverflow.com and hoping to get an answer that will help me furtherPython的基本字符串數組

+2

文檔可以 - HTTP: //docs.python.org/2.3/whatsnew/section-slices.html – alexvassel

+0

對不起,我只是不知道如何調用這個方法。現在我知道:「切片符號」 – grmbl

回答

2

它訪問反向字符串。從第81字到第67字。步長值是-1。如果你指定一個正值,你什麼也得不到。

print s[81:67:-1] 
print s[81:67:1] 

輸出

taht rewsna na 

你可以閱讀更多關於它here。它被稱爲切片符號。

+0

它會反向返回字符串嗎?或者它與[[67:81]' – grmbl

+0

thx @thefourtheye]相同,在我可以接受你的答案時^^ 必須說python很漂亮,也許我會試着學習它。 – grmbl

+0

@grmbl它會按照你的猜測返回字符串。 – thefourtheye

1

您正在訪問來自81個位置的元素67,但以相反的順序(:-1做到這一點)

所以,你迭代的67位元素81