2017-02-16 28 views
-2

我是Python新手,嘗試播放python挑戰。而在第二級,代碼是: enter image description here 我不明白string.ascii_lowercase[2:] + string.ascii_lowercase[:2]是什麼意思。另外我在官方文件中找不到它。string.ascii_lowercase [2:] + string.ascii_lowercase [:2]

+5

你應該嘗試執行它。你會看到它只是把最初的兩個字母。 –

+0

是的,我執行它,我知道它的結果如何。我只是不知道這句話是如何實現這一點的。 – Xueyuan

回答

1

也許當步驟seperatly做了最好的解釋:

>>> import string 
>>> string.ascii_lowercase 
'abcdefghijklmnopqrstuvwxyz' 

>>> string.ascii_lowercase[:2] # Take the first two items from the string 
'ab' 

>>> string.ascii_lowercase[2:] # Take everything starting by the third item 
'cdefghijklmnopqrstuvwxyz' 

>>> string.ascii_lowercase[2:] + string.ascii_lowercase[:2] # concatenate them 
'cdefghijklmnopqrstuvwxyzab' 

這在official Python tutorial (in the strings section)解釋。

+0

非常感謝 – Xueyuan

+0

@Xueyuan沒問題。請不要忘記接受(和/或upvote)答案。 :) – MSeifert