string.ascii_lowercase [2:] + string.ascii_lowercase [:2]

时间:2017-02-16 12:56:27

标签: python

我是Python新手并尝试玩python挑战。在第2级,代码是: enter image description here 我不明白string.ascii_lowercase[2:] + string.ascii_lowercase[:2]的含义。我也在官方文件中找不到它。

1 个答案:

答案 0 :(得分:1)

当分步完成时,也许最好解释一下:

>>> 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)中解释了这一点。