Nearby lessons
52 of 109Python - String Methods
- Understand how string methods are called and why they are used
- Learn case conversion and case checking methods
- Learn how to search, replace and count text in a string
- Convert between strings and lists using split() and join()
- Apply string methods in small real-world programs
What Are String Methods?
A method is a function that belongs to an object.
In Python, every string has a built-in set of methods that we can call using dot notation: text.method(arguments)
Important: String methods never modify the original string. They return a new string, because strings are immutable.
1. upper() and lower()
upper() converts all characters to uppercase, and lower() converts all characters to lowercase.
2. title() and capitalize()
title() capitalizes the first letter of every word, while capitalize() capitalizes only the first letter of the string.
3. swapcase()
swapcase() converts uppercase letters to lowercase and lowercase letters to uppercase.
4. isupper() and islower()
isupper() returns True if all characters are uppercase. islower() returns True if all characters are lowercase.
5. istitle()
istitle() returns True if every word starts with an uppercase letter.
6. isalpha() and isdigit()
isalpha() returns True if the string contains only letters. isdigit() returns True if the string contains only digits.
7. isalnum()
isalnum() returns True if the string contains only letters and digits (no spaces or symbols).
8. isspace()
isspace() returns True if the string contains only whitespace characters.
9. strip()
strip() removes leading and trailing spaces from a string.
10. lstrip() and rstrip()
lstrip() removes only the left (leading) spaces, and rstrip() removes only the right (trailing) spaces.
11. strip() with a Character Argument
We can pass characters to strip() to remove them from both ends of the string.
12. find() and rfind()
find() returns the index of the first occurrence of a substring. rfind() returns the index of the last occurrence.
13. find() When the Substring Is Not Present
If the substring is not found, find() returns -1.
14. index() and rindex()
index() and rindex() work like find() and rfind(), but they raise a ValueError if the substring is not found.
15. count()
count() returns how many times a substring appears in the string.
16. replace()
replace() replaces all occurrences of a substring with another substring. A third argument limits how many replacements are made.
17. split()
split() divides a string into a list of words. By default, it splits on spaces.
18. split() with maxsplit
We can pass a separator and a maximum number of splits.
19. splitlines()
splitlines() splits a multi-line string into a list of lines.
20. join()
join() is the opposite of split(). It joins a list of strings into one string using a separator.
21. startswith() and endswith()
startswith() checks whether the string begins with a given substring. endswith() checks whether it ends with a given substring.
22. center(), ljust() and rjust()
These methods pad the string to a given width. center() places the string in the middle, ljust() on the left, and rjust() on the right.
23. zfill()
zfill() pads the string with zeros on the left until it reaches the given width.
24. Program - Reverse a String
Using slicing, we can reverse a string in one line.
25. Program - Reverse the Words of a Sentence
Split the sentence into words, reverse the list, and join it back.
26. Program - Count the Vowels
Loop through the string and count every vowel using the in operator.
27. Program - Check if a Word Is a Palindrome
A palindrome reads the same forwards and backwards.
- String methods are called with dot notation: text.method()
- String methods never modify the original string - they return a new string
- Use find() / index() to search, replace() to substitute, split() / join() to convert between strings and lists
- Methods like isupper(), isdigit() and isalpha() return True or False
- String methods are case-sensitive, so text.find('Python') and text.find('python') are different