What Are String Methods?
String methods are built-in functions associated with Python’s str type. They perform specific operations on string values and return results - like transformed text, boolean checks, counts, or search results. Since strings are immutable, these methods do not change the original string; they return a new value instead.
Common Python String Methods :
1. upper() :
Converts the whole string to upper case.
2. lower() :
Converts the whole string to lower case.
3. islower() :
Returns True if all the characters in the string are lowercase.
4. isupper() :
Returns True if all the characters in the string are uppercase.
5. title() :
Convert First Letter to Uppercase.
6. capitalize() :
Capitalize First Character.
7. strip() , lstrip() , rstrip() :
strip() method removes the leading and trailing whitespaces
lstrip() method removes the whitespaces that are left of the string
rstrip() method removes the whitespaces that are right of the string
8. replace() :
replace() method replaces the text inside string
9. split() :
Converts the string into list. By default the value is ' '. If value is given , the string is splitted based on that value.
10. join() :
Join List into String
11. find() :
It returns the index of the substring.
12. count() :
Counts the number of occurences of the given character.
13. startswith() and endswith() :
startswith() method returns true if the string starts with the specified value.
endswith() method returns true if the string ends with the specified value.
14. isnumeric(), isalpha(), isalnum()
isnumeric() - Returns True if all characters in the string are numeric
isalpha() - Returns True if all characters in the string are alphabets
isalnum() - Returns True if all characters in the string are alphanumeric
15. len() :
returns the length of the string.
| Method | Description / Purpose | Example | Output |
|---|---|---|---|
upper() | Converts string to uppercase | "python".upper() | PYTHON |
lower() | Converts string to lowercase | "PyThOn".lower() | python |
title() | Capitalizes first letter of each word | "hello world".title() | Hello World |
capitalize() | Capitalizes first character of string | "python programming".capitalize() | Python programming |
strip() | Removes leading & trailing spaces | " Python ".strip() | Python |
lstrip() | Removes spaces from left side | " Python".lstrip() | Python |
rstrip() | Removes spaces from right side | "Python ".rstrip() | Python |
replace(a, b) | Replaces substring with another | "I love Java".replace("Java","Python") | I love Python |
split() | Splits string into list (by spaces) | "Python is fun".split() | ['Python','is','fun'] |
split(',') | Splits string using custom delimiter | "a,b,c".split(',') | ['a','b','c'] |
' '.join(list) | Joins list into string | " ".join(['Python','is','fun']) | Python is fun |
find() | Returns index of substring | "Programming".find("gram") | 3 |
count() | Counts occurrences of substring | "banana".count("a") | 3 |
startswith() | Checks if string begins with value | "hello.py".startswith("hello") | True |
endswith() | Checks if string ends with value | "file.txt".endswith(".txt") | True |
isalpha() | Returns True if only letters | "abc".isalpha() | True |
isdigit() | Returns True if only digits | "12345".isdigit() | True |
isalnum() | Returns True if letters & numbers | "abc123".isalnum() | True |
len() | Returns length of string | len("Python") | 6 |