2017-02-16 62 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