String Methods

5 min read ·

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.

Convert to Lowercase (lower())

Converts all characters in a string to lowercase.

Remove Extra Spaces (strip())

Removes whitespace from the start and end of a string.

Replace Text (replace())

Replaces a specified value with another value.

Split String (split())

Splits a string into a list based on a separator.

Capitalize First Letter (capitalize())

Converts the first character to uppercase.

Title Case (title())

Converts the first letter of each word to uppercase.

Check String Content

isalpha() – Only letters


isdigit() – Only digits


isalnum() – Letters and numbers


Find Text (find())

Returns the index of the first occurrence of a value. Returns -1 if not found.

Check Start and End

startswith()


endswith()


Join Strings (join())

Joins elements of a list into a single string.

Python String Methods – Complete Table

MethodDescriptionExample
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().


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