Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 01

String Handling Overview

Review indexing, slicing, and immutability of str, then understand the built-in string methods through visual diagrams.

Beginner15 min

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.

Indexed characters in python
python
s = "python"

Memory trick: Every character has its own locker number (index).

2. Indexing → Get one character

Indexing always returns one character.

Flow diagram for s[0]
python
s = "python"

print(s[0])   # p
print(s[5])   # n
How s[0] maps to p

3. Slicing → Get multiple characters

Slicing returns a range of characters.

Visual selection for s[1:4]
python
s = "python"

print(s[1:4])   # yth
Slicing rule: start included, stop excluded

Easy memory: Start ✔ | Stop ✗

4. Why strings are immutable

Immutable means the existing string cannot be edited.

Incorrect attempt to change s[0]
python
s = "python"

# s[0] = "P"   # TypeError
String is read-only; changes need a new object

Python protects the original string and creates a new one whenever a change is needed.

5. Method call flow

String methods return a new string.

Visual workflow for upper()
python
s = "python"

print(s.upper())   # PYTHON
print(s)           # python

To permanently update:

python
s = s.upper()
Memory formula: Original + Method = New String

6. Method map (visual classification)

Instead of memorizing methods individually, organize them into five families.

str method families hub diagram
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() syntax breakdown
python
text.method()

# Save the returned value
text = text.method()
python
name = "abraham"

print(name.upper())
print(name.title())

name = name.upper()

Quick knowledge check

Q1. What is the output?

python
s = "python"
print(s[2])

Answer: t

Q2. What is the result of slicing?

python
s = "python"
print(s[1:5])

Answer: ytho

Q3. Which statement is correct?

  • upper() changes the existing string
  • upper() returns a new string ✓

30-second revision sheet

Revision: index, slice, immutable, method returns new string
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…