String Methods in python
4 min readJun 23, 2026
upper(): Converts all lowercase letters to UPPERCASE. (Non-alphabets are completely ignored).lower():Converts all uppercase letters to lowercase.title():Capitalizes the First Letter Of Every Word.capitalize():Capitalizes only the very first letter of the entire string.swapcase():Swaps the case (UPPER → lower, and lower →$ UPPER).strip()Cuts out all spaces from both the left (start) and right (end) sides.lstrip():Cuts out spaces from the Left side only.rstrip():Cuts out spaces from the Right side only.count(substring):Counts how many times a character/pattern appears.find(substring):Returns the lowest index address. Returns-1if the character is not found.index(substring):Exactly likefind(), but crashes with a ValueError if not found.split(separator):Breaks a single string into a List of multiple strings based on a separator.replace(old, new):Swaps an old character/pattern with a brand new one.isalpha():Trueif it contains only alphabets (No spaces allowed!).isdigit():Trueif it contains only numbers (0-9).isalnum():Trueif it is a mix of only alphabets and numbers.islower():Trueif all letters are lowercase.isupper():Trueif all letters are uppercase.
partition(substring)
- Always cuts the string into exactly 3 parts based on your split value.
- Layout:
(before, split_value, after)
center(len, ch)
- Shifts the string right to the center of a new line.
- Layout: Fills left and right sides with your custom character (
ch)
zfill(len)
- Pads your data with leading zeros (
0) on the left side. - Layout: Stops filling once the total length matches your
lennumber.
str.maketrans("input", "output")
- Creates a mapping table (dictionary) that connects the ASCII values of your input characters to your output characters.
- Rule: The input and output strings must have the same length
Var.translate(mapping_table)
- Uses the mapping table to actually swap out characters in your string.
- Rule: Can be used on any string to perform character translation
isupper()
- Checks whether all the characters inside a string are in uppercase.
- Rule: Returns
Trueif all letters are capital; otherwiseFalse.
. islower()
- Checks whether all the characters inside a string are in lowercase.
.️⃣ istitle()
- Checks every single word in the string.
- Rule: Returns
Trueif the 1st letter of every word is uppercase and the remaining letters are lowercase.
Get Vinod Gosala’s stories in your inbox
Join Medium for free to get updates from this writer.
. isdigit()
- Returns
Trueif all the elements in the string are numbers (0-9). Otherwise, returnsFalse.
. isalpha()
- Returns
Trueif all the elements in the string are alphabets (a-z,A-Z). (No spaces or numbers allowed!)
. isalnum()
- Returns
Trueif the string contains a clean mix of only alphabets, only digits, or both (no special characters like@,#, or spaces).
isspace()
- Checks if the string contains only whitespace characters (spaces, tabs, or newlines).
- Output: Returns
Trueif it is completely empty space; otherwiseFalse.
. isidentifier()
- Checks whether a string can be used as a valid Variable Name / Identifier according to Python’s naming conventions.
- Rule: Returns
Trueif it only contains letters, numbers, and underscores, and does not start with a number.
. startswith(substring)
- Checks if a string sequence begins with a specific character or phrase.
- Output: Returns
Trueif the prefix matches; otherwiseFalse.
. endswith(substring)
- Checks if a string sequence finishes with a specific character or phrase.
- Output: Returns
Trueif the suffix matches; otherwiseFalse
isspace()
- In Short: Checks if the string contains only whitespace characters (spaces, tabs, or newlines).
- Output: Returns
Trueif it is completely empty space; otherwiseFalse.
. isidentifier()
- Checks whether a string can be used as a valid Variable Name / Identifier according to Python’s naming conventions.
- Rule: Returns
Trueif it only contains letters, numbers, and underscores, and does not start with a number.
. startswith(substring)
- Checks if a string sequence begins with a specific character or phrase.
- Output: Returns
Trueif the prefix matches; otherwiseFalse.
. endswith(substring)
- Checks if a string sequence finishes with a specific character or phrase.
- Output: Returns
Trueif the suffix matches; otherwiseFalse
. isspace()
- Checks if the string contains only whitespace characters (spaces, tabs, or newlines).
- Output: Returns
Trueif it is completely empty space; otherwiseFalse.
. isidentifier()
- Checks whether a string can be used as a valid Variable Name / Identifier according to Python’s naming conventions.
- Rule: Returns
Trueif it only contains letters, numbers, and underscores, and does not start with a number.
. startswith(substring)
- Checks if a string sequence begins with a specific character or phrase.
- Output: Returns
Trueif the prefix matches; otherwiseFalse.
. endswith(substring)
- Checks if a string sequence finishes with a specific character or phrase.
- Output: Returns
Trueif the suffix matches; otherwiseFalse.






