I am somewhat of a python/programming newbie, and I have just been playing around with string slicing. So the simple string reverse method of string[::-1]
works just fine as we know, but there are other instances in my code below that yields unexpected results:
In [1]: string = "Howdy doody"
In [2]: string[::]
Out[2]: 'Howdy doody'
In [3]: string[::-1]
Out[3]: 'ydood ydwoH'
In [4]: string[0:]
Out[4]: 'Howdy doody'
In [5]: string[0::-1]
Out[5]: 'H' # what up with this?
In [6]: string[:len(string)]
Out[6]: 'Howdy doody'
In [7]: string[:len(string):-1]
Out[7]: '' # what up with this too?
In [8]: string[0:len(string)]
Out[8]: 'Howdy doody'
In [9]: string[0:len(string):-1]
Out[9]: '' # And what up here too.
I have commented on the lines above where I expected the strings to be reversed, but I am surprised to see why they don't simply reverse the string. Anyone know what is up with that?
Method 4: Reverse string in Python using an extended slice Extended slice offers to put a “step” field as [start, stop, step], and giving no field as start and stop indicates default to 0 and string length respectively, and “-1” denotes starting from the end and stop at the start, hence reversing a string.
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".
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.
In all of your cases which aren't working, you're starting at the beginning and then moving backwards. There is no further backwards than the beginning, so you don't get any more characters.
This would be the solution:
>>> string[len(string)::-1]
'ydood ydwoH'
The slice notation is start
, end
, step
. Where step
is negative, start
should be greater than end
to get anything. Where it's left blank ([::-1]
), that nuance is followed automatically
In the case where you only got 'H'
, it can be confusing why there was anything. But think of the explanation written out:
Beginning with the character at start, increment by step until (but not including) end
Now it becomes clear that because you start with 0, the 0 character is included
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With