๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท
๐Ÿ Python

String methods

๐Ÿ“š What are String Methods? A string method is a built-in tool that transforms or inspects text. Call them with a dot: 'hello'.upper() gives 'HELLO'. Strings in Python are immutable, so methods always return a new string instead of changing the original.

8 min 10 XP Lesson 10 of 21
String methods
๐ŸŒ

Appy Saysโ€ฆ

Strings are everywhere โ€” usernames, messages, search queries, tweets. Python gives you a powerful toolkit to slice, clean, search, and transform text with just one line of code. Master string methods and your apps can handle real user input like a pro.

๐Ÿ“–

What are String Methods?

String methods are built-in functions attached to every string in Python. You call them with a dot: "hello".upper() returns "HELLO". They never change the original string โ€” they always return a new string.

  • โ€ข.upper() / .lower() โ€” change case
  • โ€ข.strip() โ€” remove leading/trailing whitespace
  • โ€ข.replace(old, new) โ€” swap one piece of text for another
  • โ€ข.split(separator) โ€” break a string into a list
  • โ€ข.join(list) โ€” stitch a list back into a string
  • โ€ข.find(text) โ€” returns the index of the first match (or -1)
  • โ€ขin keyword โ€” "cat" in "concatenate" returns True
๐ŸŽฎ

Think of it like a Minecraft sign editor

Imagine a sign in Minecraft with text on it. String methods are like the editing tools โ€” capitalise, erase a word, find a specific letter, break the line into two lines. Each tool does one job and gives you a new, edited sign without destroying the original.

โš™๏ธ

How It Works

  • โ€ข1. A string is an object in Python โ€” it has methods baked in
  • โ€ข2. Call a method with a dot: my_string.method()
  • โ€ข3. The method returns a new string โ€” assign it: clean = name.strip()
  • โ€ข4. Chain methods together: " Hello ".strip().lower() โ†’ "hello"
  • โ€ข5. Use .split() to turn "a,b,c" into ["a", "b", "c"]
  • โ€ข6. Use ",".join(["a","b","c"]) to turn a list back into "a,b,c"
๐ŸŒ

Real-World Examples

  • โ€ขWhatsApp uses .strip() to remove accidental spaces from phone numbers before matching
  • โ€ขTikTok's hashtag parser calls .split('#') to extract tags from a caption
  • โ€ขSpotify search lowercases both the query and song titles with .lower() so 'BLINDING LIGHTS' still matches
  • โ€ขGoogle uses .replace() to sanitise user-typed URLs before fetching pages
  • โ€ขYouTube uses .find() to check if a comment contains banned words
๐Ÿ’ก

Key Facts

  • โ€ขPython strings are immutable โ€” no method changes the original; they all return new strings
  • โ€ขThere are over 40 built-in string methods in Python
  • โ€ขf"{name.title()}" combines f-strings with .title() for clean name formatting
  • โ€ข.startswith() and .endswith() are great for validating file extensions or URL prefixes
โš ๏ธ

Watch Out!

The most common mistake: forgetting to save the result. name.upper() does nothing useful unless you write name = name.upper(). String methods don't modify in-place โ€” they return a new value.

๐Ÿ“Œ

Remember

Strings are immutable โ€” methods return new strings. Always assign: result = my_string.method().

โœ…

What You Learned

  • โ€ขString methods like .upper(), .strip(), .split() transform text
  • โ€ขMethods return new strings โ€” always assign the result
  • โ€ขUnlocks: processing user input, parsing data, building search features

Key Facts

  • โ†’Python strings are immutable โ€” no method changes the original; they all return new strings
  • โ†’There are over 40 built-in string methods in Python
  • โ†’f"{name.title()}" combines f-strings with .title() for clean name formatting
  • โ†’.startswith() and .endswith() are great for validating file extensions or URL prefixes

Real-World Examples

โ€ข WhatsApp uses <code>.strip()</code> to remove accidental spaces from phone numbers before matching โ€ข TikTok's hashtag parser calls <code>.split('#')</code> to extract tags from a caption โ€ข Spotify search lowercases both the query and song titles with <code>.lower()</code> so 'BLINDING LIGHTS' still matches โ€ข Google uses <code>.replace()</code> to sanitise user-typed URLs before fetching pages โ€ข YouTube uses <code>.find()</code> to check if a comment contains banned words

Remember

Strings are immutable โ€” methods return new strings. Always assign: result = my_string.method().

Quick Quiz

1 / 2

.strip() does?