Python slice

摘自:https://stackoverflow.com/questions/21617586/reverse-string-string-1-works-but-string0-1-and-others-dont/21617612#21617612?newreg=98de46efdbe6415680e4d055fd54cc0e

Slice notation “[a:b:c]” means “count in increments of c starting at a inclusive, up to b exclusive”. If c is negative you count backwards, if omitted it is 1. If a is omitted then you start as far as possible in the direction you’re counting from (so that’s the start if c is positive and the end if negative). If b is omitted then you end as far as possible in the direction you’re counting to (so that’s the end if c is positive and the start if negative). If a or b is negative it’s an offset from the end (-1 being the last character) instead of the start.

OK, so string[0::-1] is one character, it says “count backwards from index 0 as far as you can”. As far as it can is the start of the string.

string[0:len(string):-1] or for that matter string[0:anything:-1] is subtly different. It’s empty for the same reason that string[1:0] is empty. The designated end of the slice cannot be reached from the start. You can think of this as the slice having ended “before” it began (hence is empty), or you can think of the end point being automatically adjusted to be equal to the start point (hence the slice is empty).

string[:len(string):-1] means “count backwards from the end up to but not including > > index len(string)”. That index can’t be reached, so the slice is empty.

You didn’t try string[:0:-1], but that means “count backwards from the end up to but not including index 0”. So that’s all except the first character, reversed. [:0:-1] is to [::-1] as [0:len(string)-1]is to[:]. In both cases the excluded end of the slice is the index that would have been the included last character of the slice with the end omitted.

You also didn’t try string[-1::-1], which is the same as string[::-1] because -1 means > > the last character of the string.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值