๐ Lesson 11: Python String Methods – Manipulate Text Like a Pro
Welcome to Lesson 11! Today we’ll explore string methods — built-in functions in Python that make working with text easier and faster. By the end of this lesson, you'll be able to clean, format, and analyze text efficiently.
⭐ What You Will Learn in This Lesson
- Change text case with
upper(),lower(),title(), andcapitalize() - Remove extra spaces using
strip(),lstrip(),rstrip() - Search and count text with
find(),count(), andin - Replace text, split strings into lists, and join them back
- Understand practical use cases for string methods
๐ฅ Who Is This Lesson For?
- Beginners learning Python text manipulation
- Students working with user input, files, or web data
- Anyone who wants to clean, format, or analyze text efficiently
๐ฆ What Are String Methods?
String methods are functions that belong to string objects. They let you modify, search, or analyze text without writing extra code.
๐งฉ 1. Changing Case
text = "python is Fun"
print(text.upper()) # PYTHON IS FUN
print(text.lower()) # python is fun
print(text.title()) # Python Is Fun
print(text.capitalize()) # Python is fun
๐งฉ 2. Removing Whitespace
text = " hello world "
print(text.strip()) # "hello world"
print(text.lstrip()) # "hello world "
print(text.rstrip()) # " hello world"
๐งฉ 3. Searching in Strings
text = "I love Python"
print(text.find("Python")) # 7 (index)
print(text.count("o")) # 2
print("Python" in text) # True
๐งฉ 4. Replacing and Splitting
text = "I love Python"
print(text.replace("Python", "Java")) # I love Java
words = text.split() # Split by spaces
print(words) # ['I', 'love', 'Python']
๐งฉ 5. Joining Strings
words = ["I", "love", "Python"]
sentence = " ".join(words)
print(sentence) # I love Python
๐งฉ 6. Why String Methods Matter
- Clean and format user input
- Search and analyze text
- Manipulate text for files, web, or APIs
- Write less code for common text operations
๐งช Practice
- Take a string input from the user and print it in uppercase.
- Remove extra spaces from the beginning and end of a string.
- Count how many times a specific letter appears in a string.
- Split a sentence into words and join them back with a dash ("-").
- Replace a word in a sentence with another word and print the result.
Bonus: Try combining multiple string methods in one line for advanced formatting.
❌ Common Mistakes
- Trying to modify a string directly (strings are immutable)
- Forgetting that
find()returns -1 if not found - Using
split()incorrectly without specifying a separator when needed
❓ Frequently Asked Questions (FAQ)
1. Can string methods change the original string?
No. Strings are immutable. Methods return a new string.
2. What’s the difference between strip(), lstrip(), and rstrip()?
strip() removes spaces from both ends, lstrip() from the left, and rstrip() from the right.
3. Can I chain string methods?
Yes. For example: text.strip().upper().
4. Are these methods case-sensitive?
Yes. Searching and replacing text is case-sensitive unless handled explicitly.
๐ What’s Next?
In the next lesson, you’ll learn about:
- User input in Python
- Handling input/output effectively
- Storing and using user-provided data
Comments
Post a Comment