What you'll learn
- Quick answer
- What are Python string methods?
- upper(), lower() and friends: changing case
- strip(): remove extra spaces
- split() and join(): break apart and combine
- replace(), find() and startswith(): search text
- f-strings: the clean way to format
- The Python string methods cheat sheet
- What to remember and practice next
- FAQ
Quick Answer
Python string methods are built-in tools for working with text. The ones you will use most are upper() and lower() for case, strip() to remove extra spaces, split() and join() to break up or combine text, and replace(), find(), and startswith() for searching. For putting values inside text, f-strings are the cleanest choice. Remember that strings are immutable, so every method returns a new string instead of changing the original.
What are Python string methods?
A string is just text: a name, an email, a line from a file. Python string methods are ready-made functions attached to every string that let you clean, search, and reshape that text without writing loops by hand. You call them with a dot, like name.upper().
One idea makes all of them click: strings are immutable. You cannot change a string in place. Every method returns a new string and leaves the original untouched. So you almost always store the result in a variable or use it right away.
name = "priya"
name.upper() # returns "PRIYA"
print(name) # still "priya" -- unchanged!
name = name.upper() # now name is "PRIYA"Keep that in mind and the rest of this cheat sheet is easy. If you are just starting out, our free Python course walks through strings step by step. This page is the quick reference to keep open beside it.
upper(), lower() and friends: changing case
These normalise text so comparisons behave. A common use: convert user input to one case before checking it, so "YES", "Yes", and "yes" all match.
name = "priya sharma"
print(name.upper()) # PRIYA SHARMA
print(name.lower()) # priya sharma
print(name.title()) # Priya Sharma
print(name.capitalize()) # Priya sharmatitle() capitalises the first letter of every word, while capitalize() only touches the very first letter of the string. To compare user input reliably, lower-case both sides:
answer = input("Continue? ")
if answer.lower() == "yes":
print("Great!")
strip(): remove extra spaces
Text from users and files often has stray spaces or newline characters at the ends. strip() removes whitespace from both ends. Its cousins lstrip() and rstrip() clean only the left or right side.
raw = " hello "
print(raw.strip()) # "hello"
print(raw.lstrip()) # "hello "
print(raw.rstrip()) # " hello"Gotcha: strip() only trims the ends, never the middle. It will not turn "a b" into "a b". You can also strip specific characters by passing them in:
print("###data###".strip("#")) # "data"
split() and join(): break apart and combine
This pair is the heart of everyday text work. split() turns a string into a list; join() turns a list back into a string. They are opposites.
csv = "apple,banana,mango"
fruits = csv.split(",")
print(fruits) # ['apple', 'banana', 'mango']
joined = ", ".join(fruits)
print(joined) # apple, banana, mangoCalled with no argument, split() splits on any run of whitespace, which is perfect for breaking a sentence into words:
sentence = "learn python today"
print(sentence.split()) # ['learn', 'python', 'today']Gotcha: join() is called on the separator, not the list. So it is ", ".join(fruits), not fruits.join(", "). Also, every item in the list must already be a string, or you get a TypeError.
replace(), find() and startswith(): search text
These answer the questions "swap this for that", "where is it", and "does it begin with this".
text = "I love Java"
print(text.replace("Java", "Python")) # I love Python
email = "student@priodemy.com"
print(email.find("@")) # 7 (index where @ starts)
print(email.find("xyz")) # -1 (not found)
print(email.startswith("student")) # True
print(email.endswith(".com")) # Truereplace() swaps every match by default, so "aaa".replace("a", "b") gives "bbb". Pass a count to limit it: "aaa".replace("a", "b", 1) gives "baa".
Gotcha: find() returns -1 when the text is missing, while index() raises an error instead. If you just want to know whether something is present, the in keyword reads best: if "@" in email:.
f-strings: the clean way to format
Sooner or later you need to drop values into text: a name in a greeting, a score in a message. f-strings are the modern, readable way to do it. Put an f before the quote and wrap any expression in curly braces.
name = "Aarav"
score = 92
print(f"{name} scored {score} marks") # Aarav scored 92 marksYou can format numbers inside the braces too. Here :.2f means "show two decimal places", which is handy for money:
price = 1499.5
print(f"Total: Rs {price:.2f}") # Total: Rs 1499.50You can even call methods right inside the braces, which combines nicely with everything above:
print(f"{name.upper()} passed!") # AARAV passed!f-strings need Python 3.6 or newer, which covers any recent install, so prefer them over the older % and .format() styles.
The Python string methods cheat sheet
Keep this table handy. Every method returns a new value; it does not change the original string.
| Method | What it does | Quick example → result |
|---|---|---|
upper() | ALL CAPS | "hi".upper() → "HI" |
lower() | all lowercase | "Hi".lower() → "hi" |
strip() | trim ends | " hi ".strip() → "hi" |
split() | string → list | "a,b".split(",") → ['a', 'b'] |
join() | list → string | "-".join(['a','b']) → "a-b" |
replace() | swap text | "cat".replace("c","h") → "hat" |
find() | index or -1 | "abc".find("b") → 1 |
startswith() | True / False | "abc".startswith("a") → True |
What to remember and practice next
You do not need to memorise every method. Learn the eight above plus f-strings and you can handle most real text work. When you are unsure, three habits save time:
- Store the result. Because strings are immutable, write
text = text.strip(), not justtext.strip(). - Use
infor simple checks.if "@" in email:reads better than comparingfind()to-1. - Explore in the shell. Type
dir("")in Python to list every string method, andhelp(str.strip)to read what one does.
The fastest way to remember these is to type each snippet yourself and change one thing. Reading is not the same as doing.
Ready to go deeper? Work through strings in order in our free Python course, then come back to this cheat sheet whenever you forget which method does what.
Frequently Asked Questions
Do string methods change the original string?
No. Strings in Python are immutable, which means they can never be changed in place. Every method returns a brand new string and leaves the original alone. That is why you write name = name.upper() to actually keep the result.
What is the difference between find() and index()?
Both search for text and return the position where it starts. The difference is what happens when the text is missing: find() quietly returns -1, while index() raises a ValueError and stops your program. Use find() when a missing match is normal, and the in keyword when you only need a True or False answer.
Why should I use f-strings instead of the plus sign?
Joining text with + gets messy fast and fails if a value is a number, since you cannot add a string to an integer. f-strings let you drop any value straight into the text with { }, handle numbers automatically, and even format them, like f"Rs {price:.2f}". They are shorter and much easier to read.
How do I split a sentence into words?
Call split() with no argument. It breaks the string on any run of whitespace and returns a list of words, so "learn python today".split() gives ['learn', 'python', 'today']. To split on a specific character instead, pass it in, like split(",") for comma-separated text.
Does replace() only change the first match?
No, by default replace() swaps every match in the string. If you only want to change a limited number, pass a count as the third argument: "aaa".replace("a", "b", 1) changes just the first one and returns "baa".
Where can I practise these string methods?
The best way is to type each snippet into the Python shell and change one small thing to see what happens. For a guided path from the basics onward, work through our free Python course and keep this cheat sheet open beside it as a quick reference.
