Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Python String (Backward) Slicing

Yeah I know there are a lot of similar questions up there. But I just cannot find what I was looking for.

My confusion is about the backward slicing.

my_jumble = ['jumbly', 'wumbly', 'number', 5]
print(my_jumble[:1:-1])

Now I have found that the result will be

[5, 'number']

So I thought that maybe I will test it out by changing the ends in that string slicing.

print(my_jumble[:2:-1])

I was really sure that Python would give me something like

[5, 'number', 'wumbly']

Instead it gave me this which made me totally lost...

[5]

Can someone explain what is going on here? I am new to Python and find this very confusing.. Thanks for any help.

like image 656
2Xchampion Avatar asked Mar 12 '16 10:03

2Xchampion


People also ask

Why does 1 reverse a string in Python?

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.

How do you mirror a string in Python?

Step 1: Input the string and position from we need to mirror the characters. Step 2: Creating a string which is stored in alphabetical order. Step 3: Create an empty string. Step 4: Then traverse each character up to the position from where we need a mirror and up to this sting is unchanged.


Video Answer


1 Answers

I think one of the easiest ways to understand what is going in the code is by understanding that backward slicing reverses the way you index your list (visually, it is like reversing your list) before getting sliced but the indexes of the elements in the list themselves do not change.

Thus when you have a list like this:

['jumbly', 'wumbly', 'number', 5]
    0        1         2       3   #<-- index

by making it backward reading (adding -1 as the third indexer), you make it looks like this (because it is now indexing from the last to the first, instead of from the first to the last):

[5, 'number', 'wumbly', 'jumbly']
 3      2         1       0       #<-- index

and then when you slice from the "beginning" to one (:1), you get everything from the "beginning" (now the "beginning" is 3) and stop when seeing 1:

[5, 'number', 'wumbly', 'jumbly']
 3      2         1       0       #<-- index
 ^      ^         x
 grab! grab!      nope!

Thus you got your return:

[5, 'number']

The same principle applies when you backward slice with [:2:-1]:

[5, 'number', 'wumbly', 'jumbly']
 3      2         1       0       #<-- index
 ^      x
 grab!  nope!

Thus you got your result:

[5]

Now, using that principle, you know what to put as the second indexer if you want to return what you want: zero! --> [:0:-1]:

[5, 'number', 'wumbly', 'jumbly']
 3      2         1       0       #<-- index
 ^      ^         ^       x
 grab!  grab!     grab!   nope!

Then, you will get the result that you want:

[5, 'number', 'wumbly']
like image 176
Ian Avatar answered Oct 19 '22 02:10

Ian