What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More
Python

Python String Methods You'll Use Often

Jun 30, 2026 7 Minutes Read Why Trust Us Why you can trust this guide. Written by working engineers and reviewed by our editorial team under a strict editorial policy for accuracy, clarity and zero bias. Kaustubh Saini By Kaustubh Saini Kaustubh Saini Kaustubh Saini
I'm Kaustubh Saini, founder of FavTutor. I have a genuine passion for coding and data science. In my articles, I aim to break down complex topics, share coding insights, and make learning more accessible. When I'm not writing, I'm always exploring ways to enhance your learning experience at FavTutor.
Connect on LinkedIn →
Python String Methods You'll Use Often

Knowing how to make a string is one thing. The fun starts when you begin doing things to it: tidying messy text, flipping the case, hunting for a word, or chopping a sentence into pieces. Python strings come loaded with built-in methods for exactly this, and once you know a handful of them, you can handle nearly any text task that comes up.

We'll sort the most useful ones by the job they do, case changes, whitespace trimming, search and replace, and split and join. There's one rule that connects all of them, which is that a string method always hands you back a new string. Each section has a short example you can run.

What is a string method?

A method is just a function attached to a value. You call it by writing a dot after the string, like text.upper(). The method does its work on that string and gives you a result. Since there are a lot of them, it's easier to learn them in groups rather than one big list.

String methods grouped by job: case, whitespace, search, and split or join

Changing case

The case methods switch a string to upper or lower case, or capitalise it. They earn their keep when you're cleaning up what a user typed, so a comparison works no matter how they entered it.

Case methods upper, lower, title, and capitalize with their results
print("hello".upper())        # HELLO
print("HELLO".lower())        # hello
print("hello world".title())  # Hello World
print("hello".capitalize())   # Hello
# Output:
# HELLO
# hello
# Hello World
# Hello

Here's a handy trick. Lowercase both sides before you compare text, so "YES".lower() == "yes" comes out True.

Trimming whitespace

Text that comes from people or files often carries stray spaces at the ends. The strip() method clears whitespace off both sides, while lstrip() and rstrip() trim only the left or the right.

strip() removing the spaces around a string, with notes on lstrip and rstrip
raw = "   hello   "
print(raw.strip())
print("xxhi".lstrip("x"))
# Output:
# hello
# hi

Notice you can hand strip() a character to remove, so it isn't limited to spaces. That second line strips off the leading x characters.

Searching within a string

To find where something sits, use find(). It gives back the index of the first match, or -1 when the text isn't there. To count how often something appears, use count(). And the in keyword checks membership in one go.

Searching methods find and count, replace, and the in membership test with results
word = "banana"
print(word.find("n"))   # 2
print(word.count("a"))  # 3
print("ana" in word)    # True
# Output:
# 2
# 3
# True

Replacing text

The replace() method swaps every copy of one piece of text for another, then returns the new string.

sentence = "I like cats"
print(sentence.replace("cats", "dogs"))
print("a-b-c".replace("-", " "))
# Output:
# I like dogs
# a b c

Splitting and joining

These two are mirror images of each other and they come up all the time. split() breaks a string into a list of pieces, splitting on spaces by default. join() goes the other way, gluing a list of strings back into one with a separator you pick.

split turning a string into a list and join turning a list back into a string
csv = "red,green,blue"
parts = csv.split(",")
print(parts)
print("-".join(parts))
# Output:
# ['red', 'green', 'blue']
# red-green-blue

Call split() with no argument and it breaks on any stretch of whitespace, which is just what you want for chopping a sentence into words.

Checking the contents of a string

Another group of methods answers yes-or-no questions about a string and hands back a boolean. They're great for checking that input looks the way it should.

print("12345".isdigit())   # True
print("hello".isalpha())   # True
print("Hello".startswith("He"))  # True
print("file.py".endswith(".py")) # True
# Output:
# True
# True
# True
# True

Methods return a new string

This is the rule that pulls everything together. Because strings are immutable, a method never touches the original. It returns a brand new string, so you have to save the result if you want to keep it.

Calling upper() returns a new string while the original stays unchanged unless reassigned
s = "hi"
s.upper()        # returns "HI" but does not save it
print(s)         # still "hi"
s = s.upper()    # save the result
print(s)         # now "HI"
# Output:
# hi
# HI

Forgetting to reassign is far and away the most common slip with string methods. If a method seems to have done nothing, the odds are you forgot to store what it gave back.

Chaining methods together

Since each method returns a string, you can call the next method straight onto that result. People call this chaining, and it reads from left to right.

messy = "  Hello World  "
print(messy.strip().lower().replace(" ", "_"))
# Output: hello_world

Practice exercises

Run each one and check the output against what you expected.

Clean and shout

Strip the spaces from " hi " and make it uppercase.

# Solution
text = "  hi  "
print(text.strip().upper())
# Output: HI

Split a sentence

Split "the quick brown fox" into a list of words.

# Solution
sentence = "the quick brown fox"
print(sentence.split())
# Output: ['the', 'quick', 'brown', 'fox']

Replace and save

Replace "blue" with "green" in a string and store the result.

# Solution
color = "blue car"
color = color.replace("blue", "green")
print(color)
# Output: green car

Common mistakes

  • Not saving the result. Methods return a new string; s.upper() alone does not change s.
  • Expecting find() to raise an error. It returns -1 when the text is not found, not an error.
  • Confusing split and join. split makes a list from a string; join makes a string from a list.
  • Calling join on the list. The separator owns the method: "-".join(parts), not parts.join("-").
  • Case-sensitive comparisons. "Yes" == "yes" is False; lower both sides first.

Frequently asked questions

How do I make a string uppercase or lowercase?

Use text.upper() or text.lower(). They return a new string, so save the result.

How do I remove spaces from a string?

Use strip() for both ends, or lstrip() and rstrip() for one side. You can also pass a character to remove.

How do I split a string into a list?

Call split(). With no argument it splits on whitespace; pass a separator like "," to split on that.

How do I replace text in a string?

Use replace(old, new). It swaps every occurrence and returns a new string, so reassign it.

Why does my string method seem to do nothing?

Strings are immutable, so methods don't change the original. Store the returned value, like s = s.upper().

How do I check if a string contains another string?

Use the in keyword, like "cat" in "category", or use find() to get the position.

Key takeaways

  • String methods are called with a dot, like text.upper(), and group by job.
  • Change case with upper, lower, title; trim with strip.
  • Search with find, count, and in; swap text with replace.
  • split turns a string into a list; join turns a list into a string.
  • Every method returns a new string, so save the result to keep it.

String methods are some of the most reused tools you'll meet in Python, and they let you clean and reshape almost any text. If indexing, slicing, or f-strings still feel shaky, go back and firm up the basics in Python strings, since everything here builds on them.

Kaustubh Saini
About the author

Kaustubh Saini

I'm Kaustubh Saini, founder of FavTutor. I have a genuine passion for coding and data science. In my articles, I aim to break down complex topics, share coding insights, and make learning more accessible. When I'm not writing, I'm always exploring ways to enhance your learning experience at FavTutor. Connect on LinkedIn →
Up nextPython Tuples Explained (with Examples)