Python Forum
Beginner's String Operation question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner's String Operation question
#1
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'?
Reply
#2
'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.
Reply
#3
(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.
Reply
#4
I am not sure I understand you. In this string, 'M' is at index 0.
>>> name = "Michael Jackson"
>>> name[0]
'M'
Reply
#5
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(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!
Reply
#7
That is right :) And ichabod801 also offers a nice way to interpret how indexing in Python works - it is new for me as well.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 1,799 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  NOT bit-wise operation question ajayachander 2 2,013 Jan-27-2020, 08:03 AM
Last Post: ajayachander
  *Ultra beginner math question Markg2 2 2,251 Apr-21-2019, 11:24 PM
Last Post: Markg2
  Beginner Python Homework Question (Calculate Gross Pay) matchamochi7 4 5,678 Nov-02-2018, 01:06 PM
Last Post: buran
  String to Floating Point Question Babbare 2 3,143 May-14-2017, 01:35 AM
Last Post: Babbare

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020