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.

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) - โข
inkeyword โ"cat" in "concatenate"returnsTrue
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
Remember
Strings are immutable โ methods return new strings. Always assign: result = my_string.method().
Quick Quiz
.strip() does?