Lesson 02
Case Conversion Methods
Learn capitalize(), title(), upper(), lower(), and swapcase() with visual syntax, memory tricks, and transformation flows.
Learning roadmap
You'll learn these five methods in the same order they're commonly used.
Syntax pattern (applies to every method)
s = "python"
s.upper()
s.lower()
s.title()Remember:
object.method()→ returns a new string.
1. capitalize()
Capitalizes only the first character of the entire sentence.
s = "python is an oop lang"
print(s.capitalize())
# Python is an oop langStore the result:
s = s.capitalize()Memory: One sentence → one capital.
2. title()
Capitalizes the first letter of every word.
s = "python is an oop lang"
print(s.title())
# Python Is An Oop LangMemory: Every word gets a title.
capitalize() vs title()
This is the most common interview question.
| Method | Result |
|---|---|
capitalize() |
Python is an oop lang |
title() |
Python Is An Oop Lang |
3. upper()
Converts every letter to uppercase.
s = "python"
print(s.upper())
# PYTHONDigits and symbols stay unchanged:
print("abc123".upper())
# ABC1234. lower()
Converts every letter to lowercase.
s = "Data Science"
print(s.lower())
# data scienceReal-world example:
email = "USER@GMAIL.COM"
email = email.lower()5. swapcase()
Swaps uppercase ↔ lowercase.
s = "PyThOn"
print(s.swapcase())
# pYtHoNNumbers remain unchanged:
print("12345".swapcase())
# 12345Immutability reminder
Methods create a new string. The original stays unchanged until you reassign.
Without reassignment:
s = "python"
s.upper()
print(s)
# pythonWith reassignment:
s = s.upper()
print(s)
# PYTHONPlayground example
s = "python is an oop lang"
print("capitalize:", s.capitalize())
print("title:", s.title())
print("upper:", s.upper())
print("lower:", s.lower())
print("swapcase:", "PyThOn".swapcase())Expected output:
capitalize: Python is an oop lang
title: Python Is An Oop Lang
upper: PYTHON IS AN OOP LANG
lower: python is an oop lang
swapcase: pYtHoNUse the lesson playground at the bottom of the page to run this code live.
Method summary
| Method | Effect | Memory |
|---|---|---|
capitalize() |
First character uppercase | One sentence |
title() |
Every word uppercase | Book title |
upper() |
All letters uppercase | Everything UP |
lower() |
All letters lowercase | Everything down |
swapcase() |
Flip letter casing | Reverse |
Quick knowledge check
-
Which method title-cases every word?
Answer:title() -
After
s.upper(), isschanged?
Answer: No. Only if you assign:s = s.upper() -
What happens to digits in
swapcase()?
Answer: They remain unchanged.
30-second visual revision
Interview shortcut: One → capitalize() · Every → title() · All BIG → upper() · All small → lower() · Flip → swapcase()
Next: String Validation Methods.
Loading playground…