I have a variabel s="Siva"
and I have tried doing slicing using a logic
s[0:-5:-1]
According to the concept of slicing I am going in the backward direction, so it should ideally start from "S" and then go to "a","v","i" However when i tried running this I am getting an output as only "S" and even when I tried using s[0:-100:-1] it is still showing "S". Can anyone explain why this is happening?
CodePudding user response:
The step count given by you in s[0:-5:-1] is -1, which means that string slicing will be reverse like 'a','v','i','S'. But you are starting from s[0] which is "S" and due to the step count -1,it will print the previous character from the string "Siva",But there are no characters before 'S'.That's why it's stopping and only printing 'S'. If you want the reverse of s = "Siva", then simply write s[::-1].
CodePudding user response:
Slicing is s[start:end:step]
so if you want Savi
you have to do
s[0] s[-1:0:-1]
- Start at -1 means start at the end of the string.
- End at 0 means end at the beginning ignoring this first character.
- Step -1 means go reverse one at a time.
CodePudding user response:
Indeed, slicing accepts [start:stop:step]
in its syntax. What you're saying with [0, -5, -1]
is "start at index 0; advance until index -4 (inclusive); and do so with steps of -1".
Your string is of length 4 and so index -4 is actually index 0: s[-4]
would be 'S'
.
In other words, you're basically saying: "start at index 0 and finish at index 0 (inclusive)", which is why you get only 'S'. Anything smaller than -5, for instance: -10, would also give you 'S' only because there's nowhere further to go: it's essentially the same as what would happen if you tried to do s[0:100000:1]
: you'd simply get 'Siva', because your string is 4<100000
characters long, and Python's behaviour in such cases is to just return all four (or, more generally: return as many characters as it can in "the direction of iteration", based on the sign of your step
parameter, before reaching the end of the string).
On the other hand, if you try something that is greater than -5, such as, say, -2 or even just a positive 3, you'd get an empty string: that's because you'd basically be saying "start at index -4 and advance in the negative direction until you reach something greater" – this is never expected to happen and is somewhat "gibberishy" in nature, and I guess the way Python chose to deal with it is to just return an empty string in those cases.
This was intended to answer your question of "why this happens" while granting some intuition, hopefully; when it comes to a solution, if what you want is to simply grab the first letter and then reverse the rest: I'd simply use s[0] s[-1:0:-1]
. For fun I'll note that another option would be s[0] s[1:][::-1]
.
CodePudding user response:
Slicing is used with [start:stop:step]. If you use negative numbers for start it will start at the specified index, from the end.
If you want to print "Savi", I think you must have to slices :
s="Siva"
s[0] s[-1::-1]