打印带有开始、停止和步进的字符串

2024-06-30 07:58:31 发布

您现在位置:Python中文网/ 问答频道 /正文

hello = "Hello there"
print(hello[10:0:-1])

该输出:

^{pr2}$

我也不知道为什么。在

print(hello[10:0:-2])

这个输出:

eetol

有人能帮我了解一下[10:0:-1]在做什么吗?在

谢谢!在


Tags: hellothereprintpr2eetol
1条回答
网友
1楼 · 发布于 2024-06-30 07:58:31

符号的顺序是开始、停止和步进。所以从索引10开始,转到索引0,其中端点的步长为负2,表示向后2。以下是指数图:

#                                  -
# |  H  |  e  |  l  |  l  |  o  |     |  t  |  h  |  e  |  r  |  e  |
#                                  -
#    X           5           4           3           2           1

相关问题 更多 >