Python Basics · Lesson 6
String Methods & Formatting
Learn how to clean, transform, search, split, join and format text using Python string methods and f-strings.
What you will learn
- • upper()
- • lower()
- • strip()
- • replace()
- • split()
- • join()
- • find()
- • count()
- • f-string formatting
- • Real-world text cleaning
Part 1
upper()
The upper() method converts alphabetic characters in a string to uppercase.
name = "python"
print(name.upper())CHECK YOUR UNDERSTANDING
What is the output of "python".upper()?
Complete: "python"._____()
lower()
The lower() method converts alphabetic characters to lowercase.
course = "PYTHON BASICS"
print(course.lower())CHECK YOUR UNDERSTANDING
What is the output of "PYTHON".lower()?
Complete: "PYTHON"._____()
strip()
The strip() method removes whitespace from the beginning and end of a string.
name = " Rahul "
clean_name = name.strip()
print(clean_name)CHECK YOUR UNDERSTANDING
What problem does " Rahul ".strip() solve?
Complete: user_input._____()
replace()
The replace() method replaces one piece of text with another.
text = "I like Java"
new_text = text.replace("Java", "Python")
print(new_text)CHECK YOUR UNDERSTANDING
What is the result of "I like Java".replace("Java", "Python")?
Complete: text._____("-", " ")
split()
The split() method breaks a string into smaller pieces and returns them as a list.
text = "Python Data Analytics"
words = text.split()
print(words)CHECK YOUR UNDERSTANDING
What does "Python Data Analytics".split() produce?
Complete: sentence._____()
join()
The join() method combines items from an iterable, commonly a list of strings, into one string.
words = ["Python", "Data", "Analytics"]
result = " ".join(words)
print(result)CHECK YOUR UNDERSTANDING
What is the output of " ".join(["Python", "Data"])?
Complete: "-"._____ (["2026", "09", "23"])
find()
The find() method returns the index of the first occurrence of a substring. If it is not found, it returns -1.
text = "Python"
print(text.find("t"))CHECK YOUR UNDERSTANDING
What does "Python".find("t") return?
CHECK YOUR UNDERSTANDING
What does "Python".find("z") return?
count()
The count() method counts how many times a substring occurs in a string.
text = "banana"
print(text.count("a"))CHECK YOUR UNDERSTANDING
What does "banana".count("a") return?
Complete: "banana"._____("a")
Think like a programmer
Combining string methods
String methods can be chained together to perform multiple transformations.
name = " python "
clean_name = name.strip().upper()
print(clean_name)CHECK YOUR UNDERSTANDING
What is the output of " python ".strip().upper()?
Formatting
f-strings
f-strings provide a convenient way to insert variable values directly inside a string.
name = "Aman"
age = 21
message = f"My name is {name} and I am {age}."
print(message)CHECK YOUR UNDERSTANDING
What is the main purpose of an f-string?
CHECK YOUR UNDERSTANDING
What will this print? name = "Riya"; print(f"Hello {name}")
Formatting values
product = "Laptop"
price = 55000
print(f"{product} costs ₹{price}")CHECK YOUR UNDERSTANDING
What will the above f-string display?
Debug the code
name = "Rahul"
print(name.upper)CHECK YOUR UNDERSTANDING
What is missing in name.upper?
Real-world data cleaning
Clean customer names
Data often contains unnecessary spaces or inconsistent letter casing. String methods can help prepare this data for analysis.
customer = " rahul sharma "
clean_customer = customer.strip().title()
print(clean_customer)CHECK YOUR UNDERSTANDING
What is the purpose of strip() in this example?
Note: title() is another useful string method that converts words to title case.
Practice
Complete the methods
Convert text to uppercase: text._____()
Convert text to lowercase: text._____()
Remove surrounding spaces: text._____()
Replace text: text._____("old", "new")
Split text into a list: text._____()
Count occurrences: text._____("a")
Mini Challenge
Clean a customer record
A customer entered their name with extra spaces and lowercase letters. Clean the value before displaying it.
customer = " priya pandey "
clean_customer = customer.strip().upper()
print(clean_customer)Expected output:
PRIYA PANDEYFinal Challenge
Build a text-cleaning pipeline
Imagine you receive messy customer data. Clean the name, convert it to uppercase, split the full name into words, and create a formatted message using an f-string.
customer = " rahul sharma "
clean_customer = customer.strip().upper()
words = clean_customer.split()
message = f"Customer: {clean_customer}"
print(message)
print(words)Experiment by changing the customer name and adding your own cleaning steps.
Lesson recap
- ✓ upper() converts text to uppercase.
- ✓ lower() converts text to lowercase.
- ✓ strip() removes surrounding whitespace.
- ✓ replace() replaces text.
- ✓ split() converts text into a list.
- ✓ join() combines strings.
- ✓ find() searches for text and returns its index.
- ✓ count() counts occurrences.
- ✓ f-strings insert variables into formatted text.
- ✓ String methods are useful for cleaning real-world data.