Skip to main content
Adjusted the ASCII drawing
Source Link
asiby
  • 3.5k
  • 32
  • 33

In my opinion, you will understand and memorize better the Python string slicing notation if you look at it the following way (read on).

Let's work with the following string ...

azString = "abcdefghijklmnopqrstuvwxyz"

For those who don't know, you can create any substring from azString using the notation azString[x:y]

Coming from other programming languages, that's when the common sense gets compromised. What are x and y?

I had to sit down and run several scenarios in my quest for a memorization technique that will help me remember what x and y are and help me slice strings properly at the first attempt.

My conclusion is that x and y should be seen as the boundary indexes that are surrounding the strings that we want to extra. So we should see the expression as azString[index1, index2] or even more clearer as azString[index_of_first_character, index_after_the_last_character].

Here is an example visualization of that ...

Letters   a b c d e f g h i j ...
         ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
             ┊           ┊
Indexes  0 1 2 3 4 5 6 7 8 9 ...
             |           |
cdefgh    index1       index2

So all you have to do is setting index1 and index2 to the values that will surround the desired substring. For instance, to get the substring "cdefgh", you can use azString[2:8], because the index on the left side of "c" is 2 and the one on the right size of "h" is 8.

Remember that we are setting the boundaries. And those boundaries are the positions where you could place some brackets that will be wrapped around the substring like this ...

a b [ c d e f g h ] i j

That trick works all the time and is easy to memorize.

In my opinion, you will understand and memorize better the Python string slicing notation if you look at it the following way (read on).

Let's work with the following string ...

azString = "abcdefghijklmnopqrstuvwxyz"

For those who don't know, you can create any substring from azString using the notation azString[x:y]

Coming from other programming languages, that's when the common sense gets compromised. What are x and y?

I had to sit down and run several scenarios in my quest for a memorization technique that will help me remember what x and y are and help me slice strings properly at the first attempt.

My conclusion is that x and y should be seen as the boundary indexes that are surrounding the strings that we want to extra. So we should see the expression as azString[index1, index2] or even more clearer as azString[index_of_first_character, index_after_the_last_character].

Here is an example visualization of that ...

Letters   a b c d e f g h i j ...
         ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
Indexes  0 1 2 3 4 5 6 7 8 9 ...
             |           |
cdefgh    index1       index2

So all you have to do is setting index1 and index2 to the values that will surround the desired substring. For instance, to get the substring "cdefgh", you can use azString[2:8], because the index on the left side of "c" is 2 and the one on the right size of "h" is 8.

Remember that we are setting the boundaries. And those boundaries are the positions where you could place some brackets that will be wrapped around the substring like this ...

a b [ c d e f g h ] i j

That trick works all the time and is easy to memorize.

In my opinion, you will understand and memorize better the Python string slicing notation if you look at it the following way (read on).

Let's work with the following string ...

azString = "abcdefghijklmnopqrstuvwxyz"

For those who don't know, you can create any substring from azString using the notation azString[x:y]

Coming from other programming languages, that's when the common sense gets compromised. What are x and y?

I had to sit down and run several scenarios in my quest for a memorization technique that will help me remember what x and y are and help me slice strings properly at the first attempt.

My conclusion is that x and y should be seen as the boundary indexes that are surrounding the strings that we want to extra. So we should see the expression as azString[index1, index2] or even more clearer as azString[index_of_first_character, index_after_the_last_character].

Here is an example visualization of that ...

Letters   a b c d e f g h i j ...
                  
             ┊           ┊
Indexes  0 1 2 3 4 5 6 7 8 9 ...
                        
cdefgh    index1       index2

So all you have to do is setting index1 and index2 to the values that will surround the desired substring. For instance, to get the substring "cdefgh", you can use azString[2:8], because the index on the left side of "c" is 2 and the one on the right size of "h" is 8.

Remember that we are setting the boundaries. And those boundaries are the positions where you could place some brackets that will be wrapped around the substring like this ...

a b [ c d e f g h ] i j

That trick works all the time and is easy to memorize.

Active reading.
Source Link
Peter Mortensen
  • 31.1k
  • 22
  • 111
  • 134

In my opinion, you will understand and memorize better the Python string slicing notation if you look at it the following way (read on).

Let's work with the following string ...

azString = "abcdefghijklmnopqrstuvwxyz"

For those who don't know, you can create any substring from azString using the notation azString[x:y]

Coming from other programming languages, that's when the common sense gets compromised. What are x and y?

I had to sit down and run several scenarios in my quest for a memorization technique that will help me remember what x and y are and help me slice strings properly at the first attempt.

My conclusion is that x and y should be seen as the boundary indexes that are surrounding the strings that we want to extra. So we should see the expression as azString[index1, index2] or even more clearer as azString[index_of_first_character, index_after_the_last_character].

Here is an example visualization of that ...

Letters   a b c d e f g h i j ...
         ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
Indexes  0 1 2 3 4 5 6 7 8 9 ... 
             |           |
cdefgh    index1       index2

So all you have to do if to setis setting index1 and index2 to the values that will surround the desired substring. For instance, to get the substring "cdefgh", you can use azString[2:8], because the index on the left side of "c" is 2 and the one on the right size of "h" is 8.

Remember that we are setting the boundaries. And those boundaries are the positions where you could place some brackets that will be wrapped around the substring like this ...

a b [ c d e f g h ] i j

That trick works all the time and is easy to memorize.

In my opinion, you will understand and memorize better the Python string slicing notation if you look at it the following way (read on).

Let's work with the following string ...

azString = "abcdefghijklmnopqrstuvwxyz"

For those who don't know, you can create any substring from azString using the notation azString[x:y]

Coming from other programming languages, that's when the common sense gets compromised. What are x and y?

I had to sit down and run several scenarios in my quest for a memorization technique that will help me remember what x and y are and help me slice strings properly at the first attempt.

My conclusion is that x and y should be seen as the boundary indexes that are surrounding the strings that we want to extra. So we should see the expression as azString[index1, index2] or even more clearer as azString[index_of_first_character, index_after_the_last_character].

Here is an example visualization of that ...

Letters   a b c d e f g h i j ...
         ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
Indexes  0 1 2 3 4 5 6 7 8 9 ... 
             |           |
cdefgh    index1       index2

So all you have to do if to set index1 and index2 to the values that will surround the desired substring. For instance, to get the substring "cdefgh", you can use azString[2:8] because the index on the left side of "c" is 2 and the one on the right size of "h" is 8.

Remember that we are setting the boundaries. And those boundaries are the positions where you could place some brackets that will be wrapped around the substring like this ...

a b [ c d e f g h ] i j

That trick works all the time and is easy to memorize.

In my opinion, you will understand and memorize better the Python string slicing notation if you look at it the following way (read on).

Let's work with the following string ...

azString = "abcdefghijklmnopqrstuvwxyz"

For those who don't know, you can create any substring from azString using the notation azString[x:y]

Coming from other programming languages, that's when the common sense gets compromised. What are x and y?

I had to sit down and run several scenarios in my quest for a memorization technique that will help me remember what x and y are and help me slice strings properly at the first attempt.

My conclusion is that x and y should be seen as the boundary indexes that are surrounding the strings that we want to extra. So we should see the expression as azString[index1, index2] or even more clearer as azString[index_of_first_character, index_after_the_last_character].

Here is an example visualization of that ...

Letters   a b c d e f g h i j ...
         ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
Indexes  0 1 2 3 4 5 6 7 8 9 ...
             |           |
cdefgh    index1       index2

So all you have to do is setting index1 and index2 to the values that will surround the desired substring. For instance, to get the substring "cdefgh", you can use azString[2:8], because the index on the left side of "c" is 2 and the one on the right size of "h" is 8.

Remember that we are setting the boundaries. And those boundaries are the positions where you could place some brackets that will be wrapped around the substring like this ...

a b [ c d e f g h ] i j

That trick works all the time and is easy to memorize.

Add some extra explanation.
Source Link
asiby
  • 3.5k
  • 32
  • 33

In my opinion, you will understand and memorize better the Python string slicing notation if you look at it the following way (read on).

Let's work with the following string ...

azString = "abcdefghijklmnopqrstuvwxyz"

For those who don't know, you can create any substring from azString using the notation azString[x:y]

Coming from other programming languages, that's when the common sense gets compromised. What are x and y?

I had to sit down and run several scenarios in my quest for a memorization technique that will help me remember what x and y are and help me slice strings properly at the first attempt.

My conclusion is that x and y should be seen as the boundary indexes that are surrounding the strings that we want to extra. So we should see the expression as azString[index1, index2] or even more clearer as azString[index_of_first_character, index_after_the_last_character].

Here is an example visualization of that ...

Letters   a b c d e f g h i j ...
         ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
Indexes  0 1 2 3 4 5 6 7 8 9 ... 
             |           |
cdefgh    index1       index2

So all you have to do if to set index1 and index2 to the values that will surround the desired substring. For instance, to get the substring "cdefgh", you can use azString[2:8] because the index on the left side of "c" is 2 and the one on the right size of "h" is 8.

Remember, that we are setting the boundaries. And those boundaries are the positions where you could place some brackets that will be wrapped around the substring like this ...

a b [ c d e f g h ] i j

That trick works all the time and is easy to memorize.

In my opinion, you will understand and memorize better the Python string slicing notation if you look at it the following way (read on).

Let's work with the following string ...

azString = "abcdefghijklmnopqrstuvwxyz"

For those who don't know, you can create any substring from azString using the notation azString[x:y]

Coming from other programming languages, that's when the common sense gets compromised. What are x and y?

I had to sit down and run several scenarios in my quest for a memorization technique that will help me remember what x and y are and help me slice strings properly at the first attempt.

My conclusion is that x and y should be seen as the boundary indexes that are surrounding the strings that we want to extra. So we should see the expression as azString[index1, index2] or even more clearer as azString[index_of_first_character, index_after_the_last_character].

Here is an example visualization of that ...

Letters   a b c d e f g h i j ...
         ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
Indexes  0 1 2 3 4 5 6 7 8 9 ... 
             |           |
cdefgh    index1       index2

So all you have to do if to set index1 and index2 to the values that will surround the desired substring. For instance, to get the substring "cdefgh", you can use azString[2:8] because the index on the left side of "c" is 2 and the one on the right size of "h" is 8.

Remember, we are setting the boundaries.

That trick works all the time and is easy to memorize.

In my opinion, you will understand and memorize better the Python string slicing notation if you look at it the following way (read on).

Let's work with the following string ...

azString = "abcdefghijklmnopqrstuvwxyz"

For those who don't know, you can create any substring from azString using the notation azString[x:y]

Coming from other programming languages, that's when the common sense gets compromised. What are x and y?

I had to sit down and run several scenarios in my quest for a memorization technique that will help me remember what x and y are and help me slice strings properly at the first attempt.

My conclusion is that x and y should be seen as the boundary indexes that are surrounding the strings that we want to extra. So we should see the expression as azString[index1, index2] or even more clearer as azString[index_of_first_character, index_after_the_last_character].

Here is an example visualization of that ...

Letters   a b c d e f g h i j ...
         ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
Indexes  0 1 2 3 4 5 6 7 8 9 ... 
             |           |
cdefgh    index1       index2

So all you have to do if to set index1 and index2 to the values that will surround the desired substring. For instance, to get the substring "cdefgh", you can use azString[2:8] because the index on the left side of "c" is 2 and the one on the right size of "h" is 8.

Remember that we are setting the boundaries. And those boundaries are the positions where you could place some brackets that will be wrapped around the substring like this ...

a b [ c d e f g h ] i j

That trick works all the time and is easy to memorize.

removed noise, fixed formatting
Source Link
Georgy
  • 14k
  • 7
  • 70
  • 80
Loading
Source Link
asiby
  • 3.5k
  • 32
  • 33
Loading