String Methods

Real-world data is messy: extra spaces, inconsistent casing, stray punctuation, values jammed together with commas. String methods are the cleanup crew — a built-in toolkit for searching, slicing, normalizing, and reshaping text.

Learn String Methods in our free Python course — a beginner-friendly interactive lesson with runnable examples, a practice exercise and a quick recall.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Learn the dozen methods you'll reach for daily, plus the immutability rule that catches every beginner.

Every string method returns a new string — the original is never changed. If you don't capture the return value, your "edit" vanishes. This is the number one source of confusion, so internalize it now:

Case methods are essential for normalizing user input so comparisons work no matter how someone typed something:

title() capitalizes every word; capitalize() only the very first letter. For reliable case-insensitive matching, normalize both sides with lower() .

Input from forms, files, and APIs is rarely clean. strip() and replace() are your everyday scrubbers:

Chaining is idiomatic: raw.strip().lower() reads left to right as "trim, then lowercase". Because each method returns a string, you can keep calling more methods on the result.

Checking what a string contains drives validation, filtering, and routing logic everywhere:

split() turns a string into a list; join() glues a list back into a string. Together they power CSV parsing, word counting, and reformatting:

A common gotcha: join() is a method of the separator , not the list. Read "-".join(items) as "join these items with a dash between each".

The is... family answers yes/no questions about what a string contains. They're perfect for lightweight input checks:

Note isdigit() returns False for negatives ( "-5" ) and decimals ( "3.14" ) — it literally checks that every character is a digit 0–9. For richer numeric validation you'll later use try/except around float() .

These lines should turn a raw email into a clean username. Reorder them so the final output is ADA .

Why: you must trim and lowercase the raw string (D) before searching it, or the leading spaces would offset the @ position. find (A) needs that clean value, the slice (C) needs the index, and only then can you uppercase and print (E).

hi — strings are immutable. s.upper() built a new string but the result was never captured, so s is unchanged.

b — split returns ['a', 'b', 'c'] and index [1] picks the second element.

False — the period is not a digit, so not every character qualifies and the whole test fails.

Turn a blog post title into a URL slug: lowercase, spaces become hyphens, no leading/trailing spaces.

Lesson complete — you can tame any text now!

You can change case, scrub whitespace, search, split, join, and validate strings — and you understand the immutability rule that means every method returns a fresh copy. These are the daily workhorses of text processing.

🚀 Up next: Sets — the fast, duplicate-free collection that makes membership tests instant.

Practice quiz

After s = 'hi'; s.upper(); what is the value of s?

  • 'HI'
  • None
  • 'hi'
  • Error

Answer: 'hi'. Strings are immutable; .upper() returns a new string that was discarded, so s stays 'hi'.

What does 'the QUICK brown Fox'.title() return?

  • 'The Quick Brown Fox'
  • 'THE QUICK BROWN FOX'
  • 'The quick brown fox'
  • 'the quick brown fox'

Answer: 'The Quick Brown Fox'. title() capitalizes the first letter of every word.

What is the difference between capitalize() and title()?

  • No difference
  • title() lowercases everything
  • capitalize() removes spaces
  • capitalize() only uppercases the first letter of the string; title() does every word

Answer: capitalize() only uppercases the first letter of the string; title() does every word. capitalize() affects only the very first letter; title() capitalizes each word.

What does ' hello '.strip() return?

  • ' hello '
  • 'hello'
  • 'hello '
  • ' hello'

Answer: 'hello'. strip() removes whitespace from both ends, giving 'hello'.

What does 'banana'.count('a') return?

  • 3
  • 2
  • 1
  • 0

Answer: 3. There are three 'a' characters in 'banana'.

What does 'ada@example.com'.find('zzz') return?

  • 0
  • None
  • -1
  • A ValueError

Answer: -1. find() returns -1 when the substring is not found (index() would raise ValueError).

What does 'a,b,c'.split(',') produce?

  • 'abc'
  • a
  • b
  • c

Answer: a. split(',') breaks the string on commas into a list of three items.

How do you join the list ['apple', 'banana'] with a hyphen?

  • apple
  • banana

join() is called on the separator string: '-'.join(list).

What does '3.14'.isdigit() return?

  • True
  • False
  • 3
  • Error

Answer: False. The '.' is not a digit, so isdigit() returns False.

Why must you write text = text.upper() rather than just text.upper()?

  • upper() is slow
  • upper() needs an argument
  • Strings are immutable, so methods return a new string you must capture
  • It prevents an error

Answer: Strings are immutable, so methods return a new string you must capture. String methods never modify the original; you must reassign to keep the result.