IntermediateLesson firstCategory 6 of 20

String Methods (Deep Dive)

Master advanced string manipulation methods. Read the lesson first, then move through the exercises in order.

9 Sections5 Exercises

After reading

Practice Arena

Begin with the first exercise, then continue step by step through the module.

Start with Data Normalization

Study Material

Read the full lesson

Why string methods matter

Strings are everywhere: names, messages, files, and user input. String methods help you clean and transform text without writing long loops.

Case methods

Use these when you want consistent capitalization.

python
text = "python PROGRAMMING" print(text.lower()) print(text.upper()) print(text.title())

Removing extra spaces

User input often has extra spaces.

python
msg = " Hello Python! " print(msg.strip()) print(msg.lstrip()) print(msg.rstrip())

You can also strip specific characters:

python
code = "###ERROR###" print(code.strip("#"))

Splitting text into pieces

split() breaks a string into a list.

python
csv = "apple,banana,cherry" items = csv.split(",") print(items)

Use maxsplit if you only want to split a few times.

python
record = "name:age:city" print(record.split(":", 1)) # ['name', 'age:city']

Joining text back together

join() combines a list of strings into one string.

python
words = ["Python", "is", "cool"] print(" ".join(words)) print("-".join(words))

join() only works with strings, so convert numbers first if needed.

Finding and replacing

find() gives the first index of a substring. If not found, it returns -1. replace() swaps text.

python
text = "I love Java" text = text.replace("Java", "Python") print(text) print(text.find("Python"))

Useful checks

These methods help you validate text.

python
word = "hello" print(word.startswith("he")) print(word.endswith("lo")) print(word.isalpha()) print("123".isdigit())

Common mistakes to avoid

  • Forgetting that strings are immutable (methods return new strings).
  • Calling join() on a list that contains numbers.
  • Using find() when you actually want an error (use index() for that).

What you should understand after this lesson

  • How to change case with lower(), upper(), and title().
  • How to remove extra spaces with strip().
  • How to split and join text.
  • How to replace words and find positions safely.

Interactive

Exercises for this topic

These exercises follow the exact order of the lesson. Move step-by-step from reading into coding.