Substrings
When creating substrings from strings, or for that matter subsets from lists, it is better to visualize the separators and not the entities.
For example, when looking at a string AMAZING
it is easy to see the seven letters that make up the string. And if you are trying to substring MAZ
from AMAZING
you might think you needed letter 2 through 4. 'AMAZING'[2:4]
would actually give you AZ
.
However, visualize the eight spaces around the letters:
0 1 2 3 4 5 6 7
| A | M | A | Z | I | N | G |
With that, now when you try to substring MAZ
from AMAZING
you can see you need the letters between separators 1 and 4. 'AMAZING'[1:4]
gives you MAZ
.
This different way of looking at it works for all kinds of coding, such as parsing through lists, working with arrays, etc.