1.2.11Introduction to Programming (Python)

String indexing and slicing — `s[0]`, `s[-1]`, `s[2 - 5]`, `s[ - 2]`

1,606 words7 min readdifficulty · medium

WHY does this exist?


WHAT are the rules?

![[1.2.11-String-indexing-and-slicing-—-s[0],-s[-1],-s[2-5],-s[-2].png]]


HOW to read each form (the 80/20 core)


Common Mistakes (Steel-manned)


Recall Feynman: explain to a 12-year-old

Imagine a train with cars in a line. Each car has a number painted on it: the front car is 0, then 1, 2, 3… You can also count from the back: the very last car is -1, the one before it -2. Indexing = "go fetch car number 3." Slicing = "give me cars 2 through 4" — but the rule is you stop just before the last number you said, like a fence post. And step lets you say "grab every other car." If you say a negative step, you walk the train backwards. You can look at any car, but you can't repaint it — the train (string) is frozen.


Flashcards

What does s[0] return for any non-empty string?
The first character (index 0).
For s = "PYTHON", what is s[-1]?
'N' — the last character (= s[len(s)-1]).
In s[start:stop], is stop included?
No — slicing is half-open [start, stop); stop is excluded.
How many characters does s[2:5] return (step 1)?
3 characters (5 − 2), at indices 2, 3, 4.
What does s[::2] do?
Takes every 2nd character of the whole string (start 0, step 2).
What does s[::-1] do?
Reverses the string (negative step walks backwards).
What is s[-2] for "PYTHON"?
'O' — second-from-last, equals s[4].
What error does s[99] raise on a short string?
IndexError (index out of range).
What does s[6:99] return for "PYTHON" (len 6)?
'' — slicing out of range never errors, returns empty string.
Why can't you do s[0] = 'X'?
Strings are immutable → TypeError; build a new string instead.
What is the negative-index equivalent of s[i]?
s[i - len(s)].
Default values of start, stop, step in slicing (forward)?
start=0, stop=len(s), step=1.

Connections

  • Lists indexing and slicing — same [start:stop:step] rules apply (lists are also sequences).
  • String immutability — why you can read but not assign by index.
  • For loops over strings — slicing often replaces a manual index loop.
  • String methods (split, join, replace) — combine with slicing for text processing.
  • range() function — shares the same half-open [start, stop) convention.
  • len() built-in — needed to reason about valid indices and negative indexing.

Concept Map

each char has

counted forward

counted backward

equivalent to

grab one char

grab one char

extend to range

first index

boundary

jump size

enables

makes

String ordered sequence of chars

Index address

Positive index from 0

Negative index from -1

s[-k] = s[len s - k]

Indexing s i

Slicing s start:stop:step

start included

stop EXCLUDED

step default 1

count = stop - start / step

Half-open adjacent slices fit

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, ek string basically characters ki ek line hoti hai, jaise "PYTHON". Har character ko ek address milta hai jise index kehte hain. Python mein counting 0 se shuru hoti hai — toh 'P' ka index 0 hai, 'Y' ka 1, aur aage. Last character ke liye easy trick: negative index use karo, s[-1] matlab last wala character ('N'), s[-2] second-last ('O'). Negative index basically len(s) se peeche ki taraf count karta hai.

Slicing matlab s[start:stop:step] — ek poora tukda nikalna. Yaad rakho sabse important rule: stop hamesha EXCLUDED hota hai, included nahi. Isliye s[2:5] se indices 2,3,4 milte hain yaani 'THO', index 5 wala nahi. Iska faayda — characters ki ginti seedhi: stop minus start = 5−2 = 3. step se aap skip kar sakte ho: s[::2] har doosra character leta hai ('PTO'), aur s[::-1] poori string ulti kar deta hai (reverse).

Do common galtiyan se bacho: pehli, mat sochna ki s[2:5] mein 5 included hai — nahi hai. Doosri, string ko aap badal nahi sakte (s[0]='X' error dega) kyunki strings immutable hoti hain; change karne ke liye nayi string banao jaise 'X' + s[1:]. Bas yahi 80/20 hai — index 0 se, negative end se, stop excluded, step se skip/reverse. Yeh chaar cheezein pakad lo toh text handling aasan ho jayegi.

![[audio/1.2.11-String-indexing-and-slicing-—-s[0],-s[-1],-s[2---5],-s[---2].mp3]]

![[audio/1.2.11-String-indexing-and-slicing-—-s[0],-s[-1],-s[2-5],-s[-2].mp3]]

Go deeper — visual, from zero

Test yourself — Introduction to Programming (Python)

Connections