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. Returns -1 if not found.
index() and rindex()
Similar to
find() but
throws an error if not found.
Trimming & Alignment Methods
strip(), lstrip(), rstrip()
Remove unwanted spaces.
center(), ljust(), rjust()
Align text within a given width.
Joining & Splitting Methods
split() and rsplit()
Split text into a list based on a separator.
join()
Join elements of a list into a single string.
splitlines()
Split text at line breaks.
Validation & Checking Methods
isalnum(), isalpha(), isdigit()
Check content type (alphanumeric, alphabetical, or digits).
islower() and isupper()
Check text case.
isspace() and isprintable()
Check for whitespace and printable characters.
startswith() and endswith()
Check how a string begins or ends.
Replacement & Translation Methods
replace()
Replace part of a string.
partition() and rpartition()
Split into three parts around a separator.
translate() and maketrans()
Replace multiple characters at once.
Formatting & Padding
format() and format_map()
Insert values into strings.
zfill()
Pad numbers with zeros.