Nearby lessons

62 of 159

Python - String Indexing

📌 What You Will Learn
  • Understand what string indexing is and how it accesses individual characters
  • Use zero-based positive indexing that starts at 0
  • Use negative indexing that starts at -1
  • Iterate a string using a for loop and range(len(s))
  • Handle IndexError for out-of-range indexes
  • Apply indexing in character extraction and password validation

What Is String Indexing?

String indexing is used to access individual characters from a string. Python supports both positive and negative indexing.

Zero-Based Indexing

Python follows zero-based indexing, so the first character starts from 0.

Character Positive Index Negative Index
d0-5
u1-4
r2-3
g3-2
a4-1

Positive Indexing

Positive indexing starts from 0 and moves left to right.

🐍Code Cell
1s = "python"
2print(s[0])
3print(s[1])
4print(s[2])
Output
p
y
t

Negative Indexing

Negative indexing starts from -1 and moves right to left.

🐍Code Cell
1s = "python"
2print(s[-1])
3print(s[-2])
4print(s[-3])
Output
n
o
h

Accessing First and Last Characters

🐍Code Cell
1s = "durga"
2 
3print("First Character:", s[0])
4print("Last Character:", s[-1])
Output
First Character: d
Last Character: a

Using Variables as Index

We can use variables as index values.

🐍Code Cell
1s = "python"
2i = 3
3print(s[i])
Output
h

Index Out of Range

If index exceeds string range, IndexError occurs.

🐍Code Cell
1s = "python"
2print(s[100])
3print(s[-100])
Output
IndexError: string index out of range

Iterating String Using Index

We can access characters one by one using a loop and indexing.

🐍Code Cell
1s = "python"
2 
3for i in range(len(s)):
4 print(s[i])
Output
p
y
t
h
o
n

Reverse Order Traversal

We can access characters in reverse order using negative indexing.

🐍Code Cell
1s = "python"
2 
3for i in range(-1, -len(s)-1, -1):
4 print(s[i])
Output
n
o
h
t
y
p

Difference Between Positive and Negative Indexing

Positive Indexing Negative Indexing
Left to rightRight to left
Starts from 0Starts from -1
🐍Code Cell
1s = "python"
2print(s[0])
3print(s[-6])
Output
p
p

Real World Usage

String indexing is useful for character extraction, password validation, data processing, parsing text, and pattern matching.

Program to Display Characters with Positive and Negative Index

This program prints each character along with its positive and negative index, matching the Word file example.

🐍Code Cell
1s = input("Enter Some String:")
2i = 0
3for x in s:
4 print("The character present at positive index {} and at negative index {} is {}".format(i, i-len(s), x))
5 i = i + 1
Output
Enter Some String: durga
The character present at positive index 0 and at negative index -5 is d
The character present at positive index 1 and at negative index -4 is u
The character present at positive index 2 and at negative index -3 is r
The character present at positive index 3 and at negative index -2 is g
The character present at positive index 4 and at negative index -1 is a
📝 Key Takeaways
  • String indexing accesses individual characters with square brackets
  • Positive indexes start at 0 and move left to right
  • Negative indexes start at -1 and move right to left
  • An index outside the string range raises IndexError
  • Indexing can be combined with loops and variables for traversal

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4