Modifying Strings0%
Modifying Strings
Beginner8 min read•Updated: 2026-09-03
Modifying Strings
String methods transform text and return brand-new string objects.
Key Concepts & Detailed Explanation
Common modification methods:
- .upper() / .lower(): Convert entirely to uppercase / lowercase.
- .strip(): Strip leading and trailing whitespace.
- .replace(old, new): Replace substring occurrences.
- .title(): Capitalize the first letter of each word.
- .capitalize(): Capitalize the first letter of the entire string.
Code Examples & Output
Python
raw_text = " python programming masterclass "
print("Upper:", raw_text.strip().upper())
print("Title:", raw_text.strip().title())
print("Replaced:", raw_text.replace("masterclass", "bootcamp").strip())
Expected Output:
Output
Upper: PYTHON PROGRAMMING MASTERCLASS
Title: Python Programming Masterclass
Replaced: python programming bootcamp
Best Practices & Common Pitfalls
Always call .strip() on user input before validating or storing in a database.
Practice Quiz
1. Which method eliminates whitespace from both ends of a string?
- A) trim()
- B) strip()
- C) clean()
- D) cut()
Answer: B
Explanation: strip() removes whitespace from both ends.
2. What does 'hello world'.title() produce?
- A) Hello world
- B) HELLO WORLD
- C) Hello World
- D) hello World
Answer: C
Explanation: title() capitalizes each word, producing 'Hello World'.
Practice Challenge
Clean up a messy input ' jOhN dOE ' to display 'John Doe'.
Next Lesson
Concatenation & Formatting (f-string, format, %)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Slicing & Indexing | Concatenation & Formatting (f-string, format, %) |
Practice Quiz
Test your understanding of this lesson with 1 questions. Each question has one correct answer.