String Methods in Python
A string method is a built-in function in Python that we call on a string to perform specific tasks. These methods help us modify, format, search, validate and analyze string data efficiently.
For better understanding, we have grouped these methods into categories based on their functionality.
Categories of String Methods:
- Commonly Used Methods — frequently used methods across all tasks.
- Case Conversion Methods — to change the case of letters.
- Alignment Methods — to align strings with padding.
- Search & Count Methods — to find or count substrings.
- Validation Methods — to check string properties.
- Modification / Replace Methods — to update or clean strings.
1. Commonly Used String Methods
Here is a list of commonly used string methods that are useful in almost every Python program.
upper(),lower()— for case formattingstrip(),replace()— for cleaning and editingsplit(),join()— for conversion between string and listfind(),count()— for searchingstartswith(),endswith()— for checksisdigit(),isalpha()— for validation
Click the next page to explore these commonly used methods in detail with examples, outputs and explanations.
2. Case Conversion Methods
These methods are used to change the letter case in a string.
upper()→ Converts all characters to uppercase.lower()→ Converts all characters to lowercase.title()→ Capitalizes the first letter of each word.capitalize()→ Capitalizes only the first character of the string.swapcase()→ Converts uppercase to lowercase and vice versa.
Click the next page to explore Case Conversion methods in detail with examples, outputs and explanations.
3. Alignment Methods
These methods help align a string within a specified width using padding characters.
center(width)→ Centers the string in a given width.ljust(width)→ Left aligns the string in a given width.rjust(width)→ Right aligns the string in a given width.zfill(width)→ Pads the string on the left with zeros.
Click the next page to explore Alignment methods in detail with examples, outputs and explanations.
4. Search & Count Methods
These methods are used to search for substrings or count occurrences.
find(sub)→ Returns index of first occurrence; returns -1 if not found.rfind(sub)→ Returns last occurrence index.index(sub)→ Likefind()but raises error if not found.rindex(sub)→ Likerfind()but raises error if not found.count(sub)→ Returns number of times a substring occurs.
Click the next page to explore Search & Count methods in detail with examples, outputs and explanations.
5. Validation Methods (Checking)
These methods return True or False based on the content of the string.
isalpha()→ Checks if all characters are alphabetic.isdigit()→ Checks if all characters are digits.isalnum()→ Checks if all characters are alphanumeric.isspace()→ Checks if the string contains only whitespaces.isupper()→ Checks if all characters are uppercase.islower()→ Checks if all characters are lowercase.istitle()→ Checks if the string follows title case.startswith(sub)→ Checks if string starts with given substring.endswith(sub)→ Checks if string ends with given substring.
Click the next page to explore Validation methods in detail with examples, outputs and explanations.
6. Modification / Replace Methods
These methods are used to modify, clean or replace parts of a string.
replace(old, new)→ Replaces all occurrences of a substring.strip()→ Removes whitespace from both ends.lstrip()→ Removes whitespace from the left side.rstrip()→ Removes whitespace from the right side.split()→ Splits the string into a list.join()→ Joins list elements into a single string.
Click the next page to explore Modification methods in detail with examples, outputs and explanations.