Course Progress9% complete
🐍 Python Basics Topic 9 / 100
⏱ 9 min read

String Methods

Built-in tools to manipulate, search, clean, and transform strings — the methods you will use in almost every Python program.

"Python gives you a powerful toolkit for text. Learn these methods once and you will use them in almost every program you write."

— ShurAI

What are String Methods?

A method is a built-in function that belongs to a specific type. String methods are tools Python gives you to work with text — to clean it, search it, split it, or transform it. You call them using dot notation: string.method().

python
name = "  shurai python  "

# dot notation — variable.method()
print(name.upper())   # "  SHURAI PYTHON  "
print(name.strip())   # "shurai python"
print(name)           # "  shurai python  " — original unchanged!
Methods Always Return a New String

String methods never change the original. They return a modified copy. To keep the result, save it to a variable: clean = name.strip(). This is because strings are immutable in Python.

Case Methods

python
text = "hello world"

print(text.upper())       # HELLO WORLD
print(text.lower())       # hello world
print(text.title())       # Hello World  — first letter of each word
print(text.capitalize())  # Hello world  — only very first letter
print(text.swapcase())   # HELLO WORLD  — flips all cases

Cleaning Methods — strip()

Essential when handling user input — removes unwanted spaces, tabs, or newlines:

python
messy = "   Hello World   "

print(messy.strip())    # "Hello World"   — removes both sides
print(messy.lstrip())   # "Hello World   " — removes left only
print(messy.rstrip())   # "   Hello World" — removes right only

Searching Methods

Find whether text exists, where it is, and how many times it appears:

python
sentence = "Python is great for AI and data science"

# Check if text exists — returns True or False
print("great" in sentence)            # True
print("Java" in sentence)             # False

# Find position — returns -1 if not found
print(sentence.find("great"))         # 10
print(sentence.find("Java"))          # -1

# Count occurrences
print(sentence.count("a"))            # 4

# Check start and end
print(sentence.startswith("Python"))  # True
print(sentence.endswith("science"))  # True

Replace Method

Replace every occurrence of one text with another. Case sensitive:

python
text = "I love cats. cats are the best."

print(text.replace("cats", "dogs"))
# I love dogs. dogs are the best.

# Remove a word by replacing with empty string
print(text.replace("cats", ""))
# I love . are the best.

# Replace only the first occurrence
print(text.replace("cats", "dogs", 1))
# I love dogs. cats are the best.

Split and Join — Very Powerful

split() breaks a string into a list. join() glues a list back into a string. You will use these constantly:

python
# split() — string into list
csv    = "apple,banana,mango,guava"
fruits = csv.split(",")
print(fruits)    # ['apple', 'banana', 'mango', 'guava']

words = "Hello World Python".split()   # split on spaces by default
print(words)     # ['Hello', 'World', 'Python']

# join() — list back into string
joined = " | ".join(fruits)
print(joined)    # apple | banana | mango | guava

# Common pattern: split, process, rejoin
sentence = "the quick brown fox"
words    = sentence.split()
upper    = [w.title() for w in words]
print(" ".join(upper))   # The Quick Brown Fox

Check Methods — the is...() Family

python
print("hello".isalpha())   # True  — only letters
print("12345".isdigit())   # True  — only digits
print("hi5".isalnum())     # True  — letters and/or digits
print("  ".isspace())      # True  — only whitespace
print("HELLO".isupper())   # True  — all uppercase
print("hello".islower())   # True  — all lowercase

Chaining Methods Together

Because each method returns a string, you can chain multiple methods on one line:

python
raw = "  SNEHA kulkarni  "

# strip removes spaces, then title makes it neat
clean = raw.strip().title()
print(clean)    # Sneha Kulkarni

email = "  USER@GMAIL.COM  "
print(email.strip().lower())   # user@gmail.com

Real Example — Clean and Validate User Input

python
# Simulate messy user input
raw_name  = "  SNEHA kulkarni  "
raw_email = "  Sneha@GMAIL.COM  "
raw_phone = "9876543210abc"

name  = raw_name.strip().title()
email = raw_email.strip().lower()
phone = raw_phone.strip()

print(f"Name  : {name}")     # Name  : Sneha Kulkarni
print(f"Email : {email}")    # Email : sneha@gmail.com

# Validate phone — must be digits only, 10 chars
if phone.isdigit() and len(phone) == 10:
    print("Phone : Valid")
else:
    print("Phone : Invalid — must be 10 digits")
# Phone : Invalid — must be 10 digits

"strip, lower, replace, split, join — these five alone will solve half the text problems you will ever encounter in real Python code."

— ShurAI

🧠 Quiz — Question 1

What does "hello world".title() return?

🧠 Quiz — Question 2

What does "a,b,c".split(",") return?

🧠 Quiz — Question 3

What does " hello ".strip() return?

🧠 Quiz — Question 4

What does "cats and cats".replace("cats","dogs") return?