Every Character Has a Number
The problem...
You have a string. You know it stores text.
But what if you need just one character from it?
Not the whole thing. Just one piece.
The idea!
Every character in a string has a position. A number that tells Python exactly where it is.
That number is called an index.
Making it real
Think of it like this: a string is a row of seats. Each seat has a number.
The first seat is not number 1. It's number 0.
That's how Python counts.
In practice
name = "Bull"
print(name[0])
Output → B
You use square brackets with the index number to access a character.
The full picture
name = "Bull"
# B u l l
# 0 1 2 3
name[0]→Bname[1]→uname[2]→lname[3]→l
Going further
Python also lets you count from the end. Using negative numbers.
name = "Bull"
print(name[-1])
Output → l
-1 is always the last character. -2 is the one before that. And so on.
What's really happening
You're not guessing where a character is. You're pointing at it directly.
Python goes exactly to that position and returns what's there.
Heads up!
- Counting starts at
0, not1— this trips up almost every beginner - If you use an index that doesn't exist, Python throws an error
- Spaces are characters too — they have an index
The mindset shift
Stop thinking: "It's just text."
Start thinking: "Every character has a position. And I can reach any of them."
What you should understand now
- Every character in a string has an index
- Indexes start at
0 - Use square brackets to access a character
- Negative indexes count from the end
- Spaces and symbols have indexes too