Nearby lessons

60 of 159

Python - String Methods

📌 What You Will Learn
  • 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.

Loading editor…

2. title() and capitalize()

title() capitalizes the first letter of every word, while capitalize() capitalizes only the first letter of the string.

Loading editor…

3. swapcase()

swapcase() converts uppercase letters to lowercase and lowercase letters to uppercase.

Loading editor…

4. isupper() and islower()

isupper() returns True if all characters are uppercase. islower() returns True if all characters are lowercase.

Loading editor…

5. istitle()

istitle() returns True if every word starts with an uppercase letter.

Loading editor…

6. isalpha() and isdigit()

isalpha() returns True if the string contains only letters. isdigit() returns True if the string contains only digits.

Loading editor…

7. isalnum()

isalnum() returns True if the string contains only letters and digits (no spaces or symbols).

Loading editor…

8. isspace()

isspace() returns True if the string contains only whitespace characters.

Loading editor…

9. strip()

strip() removes leading and trailing spaces from a string.

Loading editor…

10. lstrip() and rstrip()

lstrip() removes only the left (leading) spaces, and rstrip() removes only the right (trailing) spaces.

Loading editor…

11. strip() with a Character Argument

We can pass characters to strip() to remove them from both ends of the string.

Loading editor…

12. find() and rfind()

find() returns the index of the first occurrence of a substring. rfind() returns the index of the last occurrence.

Loading editor…

13. find() When the Substring Is Not Present

If the substring is not found, find() returns -1.

Loading editor…

14. index() and rindex()

index() and rindex() work like find() and rfind(), but they raise a ValueError if the substring is not found.

Loading editor…

15. count()

count() returns how many times a substring appears in the string.

Loading editor…

16. replace()

replace() replaces all occurrences of a substring with another substring. A third argument limits how many replacements are made.

Loading editor…

17. split()

split() divides a string into a list of words. By default, it splits on spaces.

Loading editor…

18. split() with maxsplit

We can pass a separator and a maximum number of splits.

Loading editor…

19. splitlines()

splitlines() splits a multi-line string into a list of lines.

Loading editor…

20. join()

join() is the opposite of split(). It joins a list of strings into one string using a separator.

Loading editor…

21. startswith() and endswith()

startswith() checks whether the string begins with a given substring. endswith() checks whether it ends with a given substring.

Loading editor…

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.

Loading editor…

23. zfill()

zfill() pads the string with zeros on the left until it reaches the given width.

Loading editor…

24. Program - Reverse a String

Using slicing, we can reverse a string in one line.

Loading editor…

25. Program - Reverse the Words of a Sentence

Split the sentence into words, reverse the list, and join it back.

Loading editor…

26. Program - Count the Vowels

Loop through the string and count every vowel using the in operator.

Loading editor…

27. Program - Check if a Word Is a Palindrome

A palindrome reads the same forwards and backwards.

Loading editor…
📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.