Python String Methods
Python strings come with a rich collection of built-in methods that allow you to analyze, modify, format, and validate text efficiently.
Important Note:
Strings in Python are immutable. This means every string method returns a new string and never alters the original one.
Commonly Used Python String Methods
Below is a categorized explanation of important string methods with clear examples.
Case Conversion Methods
capitalize()
Makes only the first character uppercase.
lower() and upper()
Convert strings to lowercase or uppercase.
swapcase()
Switches uppercase letters to lowercase and vice versa.
title()
Capitalizes the first letter of each word.
Searching & Counting Methods
count()
Counts how many times a substring appears.
find() and rfind()
Find the position of a substring.
index()
Similar to find() but throws an error if not found.
Trimming & Alignment Methods
strip(), lstrip(), rstrip()
Remove unwanted spaces from the beginning or end.
center(), ljust(), rjust()
Align text within a given width using a character.
Joining & Splitting Methods
split()
Split text into a list based on a delimiter.
join()
Join elements of a list into a single string.
splitlines()
Split text at line breaks.
Validation & Checking Methods
isalnum(), isalpha(), isdigit()
Check if the string consists of alphanumeric, alphabetic, or digit characters.
islower() and isupper()
Check if all characters are in lowercase or uppercase.
startswith() and endswith()
Check how a string begins or ends.
Replacement & Translation Methods
replace()
Replace occurrences of a substring with another.
partition()
Split into three parts: (before, separator, after).
translate() and maketrans()
Replace multiple characters at once using a translation table.
Formatting & Padding
format()
Insert values into placeholders within a string.
zfill()
Pad a numeric string with zeros on the left until a specific length is reached.