Python Forum
Beginner's String Operation question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Beginner's String Operation question (/thread-13152.html)



Beginner's String Operation question - docboy - Sep-30-2018

I'm taking an intro Python course online.

For the code --> name="Michael Jackson" I can see why for name[0:5:2] the output is "Mca." 'M' is indexed as Zero and the 'e' is indexed as 5. So every 2nd letter is Mca, with the 'e' being the 5th character. So the 'e' is not counted, being it is an odd number.

Can someone explain why name[0:4:2] the output is not "Mca" The output I get is "Mc" It would seem that since the fourth character is 'a', if counting 'M' as ZERO, the 'a' should be part of the every 2nd character from 'M' until 'a'

In other words, why does name[0:5:2] and name[0:4:2] not both yield 'Mca'?


RE: Beginner's String Operation question - j.crater - Sep-30-2018

'e' is not being counted becuase it's outside of the [0:5] range, not because it is an odd number. It may be a bit confusing until you memorize how slicing works. If you slice an iterable variable with [begin:end], begin indexed item is included, while the end one is not. If you have [0:4], you can understand it as 4 items being selected, if that helps.


RE: Beginner's String Operation question - docboy - Sep-30-2018

(Sep-30-2018, 05:16 PM)j.crater Wrote: 'e' is not being counted becuase it's outside of the [0:5] range, not because it is an odd number. It may be a bit confusing until you memorize how slicing works. If you slice an iterable variable with [begin:end], begin indexed item is included, while the end one is not. If you have [0:4], you can understand it as 4 items being selected, if that helps.

So for slicing, the index count for 'Michael Jackson' the 'M' is counted as One? That would make sense.


RE: Beginner's String Operation question - j.crater - Sep-30-2018

I am not sure I understand you. In this string, 'M' is at index 0.
>>> name = "Michael Jackson"
>>> name[0]
'M'



RE: Beginner's String Operation question - ichabod801 - Sep-30-2018

The best way to think of it is that the indexes are between letters. So in the string 'Ichabod', the 0 index is before the letter I, the 1 index is between I and c, the 2 index is between c and h, and so on. Then 'Ichabod'[0:3] gives 'Ich', since 3 is between the h and the a. Then you just have to see a single index (Ichabod[2]) as picking up the single item after that index.


RE: Beginner's String Operation question - docboy - Sep-30-2018

(Sep-30-2018, 05:16 PM)j.crater Wrote: 'e' is not being counted becuase it's outside of the [0:5] range, not because it is an odd number. It may be a bit confusing until you memorize how slicing works. If you slice an iterable variable with [begin:end], begin indexed item is included, while the end one is not. If you have [0:4], you can understand it as 4 items being selected, if that helps.

Ok, I had to re read your question. Think I get it now. The key is you pointing out the end index is NOT included. Thanks!


RE: Beginner's String Operation question - j.crater - Sep-30-2018

That is right :) And ichabod801 also offers a nice way to interpret how indexing in Python works - it is new for me as well.