简体   繁体   English

phyton中的切片练习

[英]Slice practice in phyton

course = 'Python for Beginners' course = 'Python 初学者'

print(course[-16:-1])打印(课程[-16:-1])

What is the result?结果是什么?

Explain the result please.请解释结果。

I tried to figure out the result without success.我试图弄清楚结果没有成功。

The slice notation [-16:-1] means go from -16 to -1 (not inclusive) in steps of +1, so it's accessing indices -16, -15, -14, -13, ... -2 in that order.切片符号[-16:-1]表示 go 从 -16 到 -1(不包含),步长为 +1,因此它访问索引 -16、-15、-14、-13、... -2那个命令。

Negative indices means to look from the end of the string and look backwards, so -1 is the last character, -2 is the second-to-last character, and so on.负索引表示从字符串的末尾往后看,所以-1 是最后一个字符,-2 是倒数第二个字符,依此类推。 If you count 16 characters from the end of "Python for Beginners", you get the "o" in "Python".如果从《Python for Beginners》的末尾算起16个字符,就得到了《Python》中的“o”。 After that, since we're counting by +1 at that point, we just grab the next letters until index -2 which is the "r" in "Beginners".在那之后,因为我们在那个时候按 +1 计数,所以我们只获取下一个字母,直到索引 -2,即“初学者”中的“r”。

The final output is on for Beginner on for Beginner

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM