Course Content
How to Add a Virtual Environment in Python
0/1
Set
0/1
Python
About Lesson

In Python, strings come with a variety of built-in methods that allow you to manipulate and interact with them easily. These methods are built into the string class and can be used directly on string objects. Here’s an overview of some of the most commonly used string methods:

  1. str.upper()
    Converts all characters in the string to uppercase.
  2. str.lower()
    Converts all characters in the string to lowercase.
  3. str.capitalize()
    Capitalizes the first character of the string and makes all other characters lowercase.
  4. str.title()
    Capitalizes the first letter of each word in the string.
  5. str.strip()
    Removes any leading and trailing whitespace from the string.
  6. str.lstrip() and str.rstrip()
    Removes whitespace (or other characters) from the left (lstrip()) or right (rstrip()) side of the string.
  7. str.replace(old, new)
    Replaces occurrences of the substring old with the substring new.
  8. str.split()
    Splits the string into a list of substrings based on a delimiter (whitespace by default).
  9. str.join(iterable)
    Joins elements of an iterable (such as a list) into a single string, with the string as the separator.
  10. str.find(sub)
    Returns the lowest index where the substring sub is found in the string. Returns -1 if the substring is not found.
  11. str.count(sub)
    Returns the number of non-overlapping occurrences of the substring sub in the string.
  12. str.startswith(prefix)
    Checks if the string starts with the specified prefix.
  13. str.endswith(suffix)
    Checks if the string ends with the specified suffix.
  14. str.isdigit()
    Returns True if all characters in the string are digits.
  15. str.isalpha()
    Returns True if all characters in the string are alphabetic (letters only).
  16. str.isalnum()
    Returns True if all characters in the string are alphanumeric (letters and digits).
  17. str.isupper() and str.islower()
    Checks if all characters in the string are uppercase (isupper()) or lowercase (islower()).
  18. str.format(*args, **kwargs)
    Used for string formatting. It allows you to embed variables or expressions inside a string template.
  19. str.partition(sep)
    Splits the string into a tuple of three elements: the part before the separator, the separator itself, and the part after the separator.
  20. str.removeprefix(prefix)
    Removes the specified prefix from the string if it exists.
  21. str.removesuffix(suffix)
    Removes the specified suffix from the string if it exists.
Python
text1 = "hello"
print(text1.upper()) # Output: "HELLO"

text2 = "HELLO"
print(text2.lower()) # Output: "hello"

text3 = "hello"
print(text3.capitalize()) # Output: "Hello"

text4 = "hello world"
print(text4.title()) # Output: "Hello World"

text5 = " hello "
print(text5.strip()) # Output: "hello"

text6 = " hello "
print(text6.lstrip()) # Output: "hello "
print(text6.rstrip()) # Output: " hello"

text7 = "hello world"
print(text7.replace("world", "Python")) # Output: "hello Python"

text8 = "hello world"
print(text8.split()) # Output: ['hello', 'world']

words = ['hello', 'world']
print(" ".join(words)) # Output: "hello world"

text9 = "hello world"
print(text9.find("world")) # Output: 6
print(text9.find("python")) # Output: -1

text10 = "hello world world"
print(text10.count("world")) # Output: 2

text11 = "hello world"
print(text11.startswith("hello")) # Output: True

text12 = "hello world"
print(text12.endswith("world")) # Output: True

text13 = "12345"
print(text13.isdigit()) # Output: True

text14 = "hello"
print(text14.isalpha()) # Output: True

text15 = "hello123"
print(text15.isalnum()) # Output: True

text16 = "HELLO"
print(text16.isupper()) # Output: True

text17 = "hello"
print(text17.islower()) # Output: True

text18 = "42"
print(text18.format("Alice")) # Output: "Hello, Alice!"

text19 = "hello-world"
print(text19.partition("-")) # Output: ('hello', '-', 'world')

text20 = "TestHook"
print(text20.removeprefix("Test")) # Output: "Hook"

text = "MiscTests"
print(text.removesuffix("Tests")) # Output: "Misc"