1

This question is not about another way to solve the problem, but about the existence of a syntax to plug a hole. I know there are many other ways to do this, but it is not what I'm interested in.

In python, you can index sequences (strings, lists or tuples) from the end with negative index value, and you can slice them using ":" inside the square brackets. for an exemple, you can use "123456789"[-4:-2] to get "67". You can even use a variable :

for i in range(-4, 0):
    print('"123456789"[-5:{}] yields "{}"'.format(i, "123456789"[-5:i]))

which will yield

"123456789"[-5:-4] yields "5"
"123456789"[-5:-3] yields "56"
"123456789"[-5:-2] yields "567"
"123456789"[-5:-1] yields "5678"

but what if you want to go up to the last position ? because print(0, "123456789"[-5:0]) won't do :

for i in range(-4, 1):
    print('"123456789"[-5:{}] yields "{}"'.format(i, "123456789"[-5:i]))

which will yield

"123456789"[-5:-4] yields "5"
"123456789"[-5:-3] yields "56"
"123456789"[-5:-2] yields "567"
"123456789"[-5:-1] yields "5678"
"123456789"[-5:0] yields ""

One way to solve this might be to got to None instead of 0, in order to emulate this code : print(i, "123456789"[-5:])

for r in range(-4, 0)), [None]:
    for i in r:
        print('"123456789"[-5:{}] yields "{}"'.format(i, "123456789"[-5:i]))

or

for i in [ x for r in [range(-4, 0)), [None] for x in r]:
    print('"123456789"[-5:{}] yields "{}"'.format(i, "123456789"[-5:i]))

which will yield

"123456789"[-5:-4] yields "5"
"123456789"[-5:-3] yields "56"
"123456789"[-5:-2] yields "567"
"123456789"[-5:-1] yields "5678"
"123456789"[-5:None] yields "56789"

but this is not a perfect solution, because None does not mean the last position, but the default value which will depend on the sign of the step and will lead to inconsistent results if step has a negative value:

for step in 1, -1:
    print("step={}".format(step))
    for r in [range(-7, -0), [None,]]:
        for i in r:
            print('"123456789"[-4:{}:{}] yields "{}"'.format(i, step, "123456789"[-4:i:step]))

will yield

step=1
"123456789"[-4:-7:1] yields ""
"123456789"[-4:-6:1] yields ""
"123456789"[-4:-5:1] yields ""
"123456789"[-4:-4:1] yields ""
"123456789"[-4:-3:1] yields "6"
"123456789"[-4:-2:1] yields "67"
"123456789"[-4:-1:1] yields "678"
"123456789"[-4:None:1] yields "6789"
step=-1
"123456789"[-4:-7:-1] yields "654"
"123456789"[-4:-6:-1] yields "65"
"123456789"[-4:-5:-1] yields "6"
"123456789"[-4:-4:-1] yields ""
"123456789"[-4:-3:-1] yields ""
"123456789"[-4:-2:-1] yields ""
"123456789"[-4:-1:-1] yields ""
"123456789"[-4:None:-1] yields "654321"

is there another way to specify that my slice goes to the last position instead of the default position ?

Camion
  • 1,264
  • 9
  • 22

4 Answers4

1

If the stop value in slice is greater than or equal to length of the string, It will be set to the length of the string by default.

"123456789"[-5:len('123456789')] also yields '56789'

alec_djinn
  • 10,104
  • 8
  • 46
  • 71
1

As mental excercise you can reverse string, grab needed slice and reverse it back. If you iterate over values :-):

in [1]: for i in range(1, 6):
...:     print('123456789'[::-1][:i][::-1])
...:
9
89
789
6789
56789
Aivar Paalberg
  • 4,645
  • 4
  • 16
  • 17
0

None is not the last item in the sequence. None is bound of sequence and when you change direction, you got expected result.

>>> s = "1234567890"
>>> s[-5::1]
'67890'
>>> s[-5::-1]
'654321'
>>> s[-5:len(s):1]
'67890'
>>> s[-5:len(s):-1]
''
0

As @Daniel Mesejo observed , if I'm ready to use the constant None, I can as well use len(s) or as @Salai Madhavan observed : any very large number, which would automagically be converted to len(s).

for step in 1, -1:
    print("step={}".format(step))
    for r in [range(-7, -0), [999999,]]:
        for i in r:
            print('"123456789"[-4:{}:{}] yields "{}"'.format(i, step, "123456789"[-4:i:step]))

will yield

step=1
"123456789"[-4:-7:1] yields ""
"123456789"[-4:-6:1] yields ""
"123456789"[-4:-5:1] yields ""
"123456789"[-4:-4:1] yields ""
"123456789"[-4:-3:1] yields "6"
"123456789"[-4:-2:1] yields "67"
"123456789"[-4:-1:1] yields "678"
"123456789"[-4:None:1] yields "6789"
step=-1
"123456789"[-4:-7:-1] yields "654"
"123456789"[-4:-6:-1] yields "65"
"123456789"[-4:-5:-1] yields "6"
"123456789"[-4:-4:-1] yields ""
"123456789"[-4:-3:-1] yields ""
"123456789"[-4:-2:-1] yields ""
"123456789"[-4:-1:-1] yields ""
"123456789"[-4:999999:-1] yields ""
Camion
  • 1,264
  • 9
  • 22