0
0
Pythonprogramming~3 mins

Why String indexing (positive and negative) in Python? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

Discover how a simple number can unlock any letter in your text instantly!

The Scenario

Imagine you have a long sentence and you want to find a specific letter or word inside it. Without a simple way to point to each letter, you might have to count every character by hand from the start every time.

The Problem

Counting letters manually is slow and easy to mess up. If you want the last letter, you have to count the whole sentence backward, which is confusing and wastes time.

The Solution

String indexing lets you quickly grab any letter by its position. Positive numbers count from the start, and negative numbers count from the end, so you can easily get letters without counting manually.

Before vs After
Before
text = 'hello'
# To get last letter manually:
last = text[len(text)-1]
After
text = 'hello'
last = text[-1]
What It Enables

It makes finding and using parts of text fast and simple, like picking fruits from a basket by their position.

Real Life Example

When typing a password, you might want to check the first and last characters quickly to meet rules. String indexing helps you do that easily.

Key Takeaways

String indexing lets you access letters by position.

Positive indexes start from the beginning (0, 1, 2...).

Negative indexes start from the end (-1, -2, -3...).