This guide explains Why String Positions Start at Zero through a focused example and practical reasoning. No prior knowledge of this specific operation is required.
Understanding the Concept
Python sequence indexes begin at zero: text[0] reads the first code point. Negative indexes count back from the end. This convention simplifies indexing and slice boundaries.
When You Would Use This
Read the first and third letters in a short word.
Step-by-Step Example
language_name = "Python"
print(language_name[0])
print(language_name[2])
print(language_name[-1])
How the Example Works
The outputs are P, t and n. Positive indexes start at zero, while -1 denotes the final element.
Example Output
P
t
n
Try It Yourself
Print the first and final letters of "Learning".
Common Beginner Mistakes
- Using index 1 when you mean the first character.
- Assuming
text[len(text)]is the last item rather than an out-of-range index. - Forgetting that empty strings have no valid character index.
Key Takeaways
- Python sequence indexes begin at zero:
text[0]reads the first code point. - The outputs are P, t and n.
- Avoid this mistake: Using index 1 when you mean the first character.