String Methods
Python provides a rich set of built-in string methods that allow you to manipulate, analyze, and transform strings easily.
These methods make working with text simple, powerful, and efficient.
All string methods return a new string, because strings in Python are immutable.
What Are String Methods?
String methods are functions that belong to string objects.
They are used to perform operations like:
- Changing case
- Searching text
- Replacing characters
- Splitting strings
- Removing extra spaces
Definition
String methods are built-in functions used to process and manipulate string data.
Convert to Uppercase (upper())
Converts all characters in a string to uppercase.
python
text = "python"
print(text.upper())
python
print("hello world".upper())
python
name = "Code Marathi"
print(name.upper())
Convert to Lowercase (lower())
Converts all characters in a string to lowercase.
python
text = "PYTHON"
print(text.lower())
python
print("HELLO PYTHON".lower())
python
language = "AWS CLOUD"
print(language.lower())
Remove Extra Spaces (strip())
Removes whitespace from the start and end of a string.
python
text = " Python "
print(text.strip())
python
name = " CodeMarathi"
print(name.strip())
python
print(" Hello World ".strip())
Replace Text (replace())
Replaces a specified value with another value.
python
text = "Hello World"
print(text.replace("World", "Python"))
python
print("I like Java".replace("Java", "Python"))
python
language = "C"
print(language.replace("C", "Python"))
Split String (split())
Splits a string into a list based on a separator.
python
text = "Python is easy"
print(text.split())
python
data = "apple,banana,orange"
print(data.split(","))
python
email = "user@gmail.com"
print(email.split("@"))
Capitalize First Letter (capitalize())
Converts the first character to uppercase.
python
text = "python"
print(text.capitalize())
python
print("code marathi".capitalize())
python
name = "aws"
print(name.capitalize())
Title Case (title())
Converts the first letter of each word to uppercase.
python
text = "python programming language"
print(text.title())
python
course = "aws cloud basics"
print(course.title())
python
print("learn python today".title())
Check String Content
isalpha() – Only letters
python
print("Python".isalpha())
python
print("Python123".isalpha())
python
print("Code".isalpha())
isdigit() – Only digits
python
print("12345".isdigit())
python
print("12a3".isdigit())
python
print("007".isdigit())
isalnum() – Letters and numbers
python
print("Python123".isalnum())
python
print("Python_123".isalnum())
python
print("123".isalnum())
Find Text (find())
Returns the index of the first occurrence of a value.
Returns
-1 if not found.python
text = "Python Programming"
print(text.find("Python"))
python
print(text.find("Java"))
python
print(text.find("gram"))
Check Start and End
startswith()
python
text = "Python Programming"
print(text.startswith("Python"))
python
print(text.startswith("Java"))
python
print("Hello".startswith("He"))
endswith()
python
print(text.endswith("Programming"))
python
print(text.endswith("Python"))
python
print("file.txt".endswith(".txt"))
Join Strings (join())
Joins elements of a list into a single string.
python
words = ["Python", "is", "easy"]
print(" ".join(words))
python
chars = ["P", "y", "t", "h", "o", "n"]
print("".join(chars))
python
data = ["2025", "09", "22"]
print("-".join(data))
Python String Methods – Complete Table
| Method | Description | Example |
|---|---|---|
upper() | Converts string to uppercase | "python".upper() |
lower() | Converts string to lowercase | "PYTHON".lower() |
capitalize() | Capitalizes first character | "python".capitalize() |
title() | Capitalizes first letter of each word | "python basics".title() |
strip() | Removes whitespace from both ends | " python ".strip() |
lstrip() | Removes whitespace from left | " python".lstrip() |
rstrip() | Removes whitespace from right | "python ".rstrip() |
replace(old, new) | Replaces substring | "hi python".replace("hi", "hello") |
split() | Splits string into list | "a b c".split() |
split(sep) | Splits using separator | "a,b,c".split(",") |
join(iterable) | Joins elements into string | " ".join(["Python","Easy"]) |
find() | Returns index of first occurrence | "python".find("p") |
rfind() | Returns last occurrence index | "python".rfind("o") |
index() | Like find() but error if not found | "python".index("p") |
count() | Counts substring occurrences | "banana".count("a") |
startswith() | Checks string start | "python".startswith("py") |
endswith() | Checks string end | "file.txt".endswith(".txt") |
isalnum() | Checks letters & numbers | "abc123".isalnum() |
isalpha() | Checks only letters | "Python".isalpha() |
isdigit() | Checks only digits | "123".isdigit() |
islower() | Checks lowercase | "python".islower() |
isupper() | Checks uppercase | "PYTHON".isupper() |
isspace() | Checks whitespace | " ".isspace() |
swapcase() | Swaps upper ↔ lower | "PyThOn".swapcase() |
center() | Centers string | "Python".center(10) |
ljust() | Left-justifies string | "Python".ljust(10) |
rjust() | Right-justifies string | "Python".rjust(10) |
zfill() | Pads with zeros | "5".zfill(3) |
format() | Formats string | "Hello {}".format("Python") |
encode() | Encodes string | "python".encode() |
Learning Tip
You don’t need to memorize all methods.
Learn the most used ones: upper(), lower(), strip(), replace(), split(), join(), find(), startswith(), endswith().
Summary
- String methods modify and analyze text
- Strings are immutable (original string not changed)
- Common methods include
upper(),lower(),strip(),replace() - Methods like
split()andjoin()handle text data - Validation methods help check string content
Exercise
- Convert a string to uppercase and lowercase
- Replace a word in a sentence
- Split a sentence into words
- Check if a string contains only digits