Commonly Used String Methods in Python
Here is a list of string methods that are most commonly used while working with strings in Python. These methods help in formatting, cleaning, converting, searching, checking, and validating strings.
Common methods types:
- Case Formatting Methods
upper()Methodlower()Method
- Cleaning and Editing Methods
strip()Methodreplace()Method
- Conversion Between String and List
split()Methodjoin()Method
- Searching Methods
find()Methodcount()Method
- Check Methods
startswith()Methodendwith()Method
- Check Methods
isdigit()Methodisalpha()Method
1. upper() Method
The upper() method is used to convert all the characters in a string to
uppercase.
It returns a new string where all lowercase alphabets are changed to their uppercase form, while
other characters (like numbers or symbols) remain unchanged.
Syntax:
string.upper()
Example:
text = "hello"
print(text.upper())
print(text)
Output:
HELLO
hello
Explanation:
- The original string is
"hello". - The
upper()method converts all characters to uppercase:"HELLO". - These methods are non-destructive, meaning they do not change the
original string. So,
print(text)will still output"hello", not the uppercase version.
2. lower() Method
The lower() method is used to convert all the characters in a string to
lowercase.
It returns a new string in which all uppercase alphabets are converted to their lowercase form.
Characters like numbers and special symbols remain unchanged.
Syntax:
string.lower()
Example:
text = "HELLO"
print(text.lower())
Output:
hello
Explanation:
- The original string is
"HELLO". - The
lower()method converts it to all lowercase:"hello".
3. strip() Method
Removes spaces (or specified characters) from both ends of a string.
Syntax:
string.strip()
Note:
If no argument is given, it removes all spaces,
tabs, and newline characters from both sides.
Example:
line = " Shiksha Sanchar "
cleaned = line.strip()
print(cleaned)
Output:
Shiksha Sanchar
Explanation:
- There were extra spaces on both sides of the string.
strip()removed those spaces but not the ones between the words.
4. replace() Method
Replaces all matching parts (substring) with a new part.
Syntax:
string.replace(old, new)
Example:
text = "Python is easy. Python is powerful."
updated = text.replace("Python", "Java")
print(updated)
Output:
Java is easy. Java is powerful.
Explanation:
- All "Python" words are replaced with "Java".
- Original string stays the same. New string is stored in
updated.
5. split() Method
Breaks the string into parts and gives a list.
Syntax:
string.split(separator)
Note:
If no separator is given, it uses space by default.
Example:
line = "Shiksha Sanchar is helpful"
words = line.split()
print(words)
Output:
['Shiksha', 'Sanchar', 'is', 'helpful']
Explanation:
- The string is split wherever there is space.
- Result is a list of words.
6. join() Method
Joins list items into one string using a separator.
Syntax:
separator.join(list)
Example:
words = ["Learn", "with", "ShikshaSanchar"]
sentence = " ".join(words)
print(sentence)
Output:
Learn with ShikshaSanchar
Explanation:
- List elements are joined with space in between.
- You can use any separator like
"-","+", etc.
7. find() Method
Searches the string for a specified value and returns
the index of the first occurrence. Returns -1 if not found.
We can also specify the start and end
positions to limit the search to a specific portion of the string.
Syntax:
string.find(substring, start, end)
Example:
text = "learning python is fun"
print(text.find("python"))
print(text.find("java"))
print(text.find("python", 0, 10)) # searches from index 0 to 9
Output:
9
-1
-1
Explanation:
"python"starts at index 9."java"is not found, so it returns-1.- Third time, it searches only from index 0 to 9, so
"python"is not found →-1.
8. count() Method
Returns the number of times a specified value occurs in the string.
Syntax:
string.count(substring, start, end)
Example:
text = "banana"
print(text.count("a"))
Output:
3
Explanation:
- The character
'a'appears 3 times in the word"banana". count()helps in frequency analysis of substrings.
9. startswith() Method
The startswith() method checks whether a string starts with a specific
prefix. It is case-sensitive means A and a are considered different.
It returns True if the string starts with the given value; otherwise, it returns
False.
Syntax:
string.startswith(prefix, start, end)
Parameters:
- prefix – The string or tuple of strings to check at the start.
- start (optional) – The position to start the search.
- end (optional) – The position to end the search.
Example:
text = "python programming"
result1 = text.startswith("py")
result2 = text.startswith("gram", 7)
print(result1)
print(result2)
Output:
True
True
Explanation:
"python programming"starts with "py" → True- From index 7 onward, string is "gramming", which starts with "gram" → True
10. endswith() Method
The endswith() method checks whether a string ends with a specific
suffix. It is case-sensitive means A and a are considered different.
It returns True if the string ends with the given value; otherwise, it returns
False.
Syntax:
string.endswith(suffix, start, end)
Parameters:
- suffix – The string or tuple of strings to check at the end.
- start (optional) – The position to start the search.
- end (optional) – The position to end the search.
Example:
text = "python programming"
result1 = text.endswith("ing")
result2 = text.endswith("thon", 0, 6)
print(result1)
print(result2)
True
True
Explanation:
"python programming"ends with "ing" → Truetext[0:6]is "python", which ends with "thon" → True
11. isalpha() Method
Returns True if all characters in the
string are alphabetic (A-Z or a-z), otherwise False.
Syntax:
string.isalpha()
Example:
text1 = "Hello"
text2 = "Hello123"
print(text1.isalpha())
print(text2.isalpha())
Output:
True
False
Explanation:
text1contains only letters, so it returnsTrue.text2has numbers, so it returnsFalse.
12. isdigit() Method
Returns True if all characters are
digits (0–9), otherwise False.
Syntax:
string.isdigit()
Example:
text1 = "123"
text2 = "123abc"
print(text1.isdigit())
print(text2.isdigit())
Output:
True
False
Explanation:
text1has only numbers, so it returnsTrue.text2has letters, so it returnsFalse.