Learn Without Walls
← Back to Python Practice Labs
Lab 6 of 10

String Methods

Python’s most underrated superpower

← Lab 5: Lists & Dicts Lab 6 of 10 Lab 7: Data →
⏳ Loading Python... (first load takes ~10 seconds)

📖 Concept Recap

Strings are immutable sequences — methods return new strings, they never modify in place.

👀 Worked Example

Real-world string cleaning — standardizing messy name data:

raw_data = [ " john smith ", "SARAH LEE", "mike_brown@email.COM", "Dr. Chen, Wei", ] def clean_name(raw): name = raw.strip() if "@" in name: name = name.split("@")[0].replace("_", " ") for title in ["Dr. ", "Mr. ", "Ms. ", "Mrs. "]: name = name.replace(title, "") if "," in name: parts = name.split(", ") name = parts[1] + " " + parts[0] return name.title() for person in raw_data: print(f"'{person}' → '{clean_name(person)}'")
✏️ Guided

Exercise 1 — Text Analyzer

Complete the text analyzer by filling in the blanks. Each blank uses a string method or variable you already have.

Output will appear here...
💡 Hint: Use .split() (no arguments) to split on any whitespace. Use the in keyword to check if "Python" appears in text.
💪 Independent

Exercise 2 — Phone Number Formatter

Write a function format_phone(raw) that takes messy phone numbers and returns them all in the format 213-555-0123. Test all 4 formats:

Output will appear here...
💡 Hint: First extract only digits: digits = ''.join(c for c in raw if c.isdigit()). Then take the last 10 digits and slice them into 3 parts: digits[-10:-7] + '-' + digits[-7:-4] + '-' + digits[-4:]
🔥 Challenge

Exercise 3 — Caesar Cipher

Write two functions:

Test: Encrypt 'Hello World' with shift=3, then decrypt it back to verify.

Output will appear here...
💡 Hint: The starter code is mostly complete — run it and study how it works. Then try different shift values and messages. Can you encrypt "Python" with shift=13 (ROT13)?
🏆 Mini Project

Mini Project — Complete Text Analyzer

Build a full text analyzer. Given the paragraph below, calculate and print all 7 metrics:

  1. Total character count (with spaces) and without spaces
  2. Word count
  3. Sentence count
  4. Average word length (in characters)
  5. The 5 most frequent words (excluding stop words: the, a, an, is, are, and, of, to, that, from, in)
  6. Percentage of words that are unique
  7. The longest word in the text
Output will appear here...
💡 Hint: Strip punctuation from words with word.strip('.,!?;:') before counting. For the top 5, sort the frequency dict by value: sorted(freq.items(), key=lambda x: x[1], reverse=True)[:5].

✅ Lab 6 Complete!

You’ve mastered string cleaning, splitting, joining, slicing, and built a Caesar cipher and full text analyzer. String skills are essential for working with any real-world data.

Continue to Lab 7: Working with Data →

← Lab 5: Lists & Dicts Lab 6 of 10 Lab 7: Data →