Lesson 01
String Handling Overview
Review indexing, slicing, and immutability of str, then understand the built-in string methods through visual diagrams.
What you will learn
| Topic | Goal |
|---|---|
| Indexing & Slicing | Access one or many characters visually |
| Immutability | Why characters cannot be modified directly |
| String Methods | Learn the important built-in str methods |
| Method Flow | Understand how methods return new strings |
For string basics (quotes, creation, sequence type), see str — String Data Type. This lesson sets up the method work in the rest of the module.
1. Strings are sequences
A string is a sequence of characters. Think of each character as living inside its own numbered box.
s = "python"Memory trick: Every character has its own locker number (index).
2. Indexing → Get one character
Indexing always returns one character.
s = "python"
print(s[0]) # p
print(s[5]) # n3. Slicing → Get multiple characters
Slicing returns a range of characters.
s = "python"
print(s[1:4]) # ythEasy memory: Start ✔ | Stop ✗
4. Why strings are immutable
Immutable means the existing string cannot be edited.
s = "python"
# s[0] = "P" # TypeErrorPython protects the original string and creates a new one whenever a change is needed.
5. Method call flow
String methods return a new string.
s = "python"
print(s.upper()) # PYTHON
print(s) # pythonTo permanently update:
s = s.upper()6. Method map (visual classification)
Instead of memorizing methods individually, organize them into five families.
| Group | Methods |
|---|---|
| Case | capitalize(), title(), upper(), lower(), swapcase() |
| Checks | isupper(), islower(), isdigit(), isalpha(), isalnum(), isspace() |
| Search | index(), count(), startswith(), endswith() |
| Trim | lstrip(), rstrip(), strip() |
| Split / Join | split(), join() |
7. Universal syntax pattern
Every string method follows the same structure.
text.method()
# Save the returned value
text = text.method()name = "abraham"
print(name.upper())
print(name.title())
name = name.upper()Quick knowledge check
Q1. What is the output?
s = "python"
print(s[2])Answer: t
Q2. What is the result of slicing?
s = "python"
print(s[1:5])Answer: ytho
Q3. Which statement is correct?
upper()changes the existing stringupper()returns a new string ✓
30-second revision sheet
| Concept | Syntax |
|---|---|
| Indexing | s[0] |
| Slicing | s[1:4] |
| Method | s.upper() |
| Reassign | s = s.upper() |
Golden rule: Index → Read · Slice → Extract · Methods → Return new · Reassign → Save
Next: Case Conversion Methods.
Loading playground…