Python String Method

Python String Methods

Python provides many built-in methods for string manipulation. Below are some of the commonly used string methods with examples.

  • str.lower(): Converts all characters in the string to lowercase.
                    
    # Example
    text = "HELLO"
    print(text.lower())  # Output: hello
                    
                
  • str.upper(): Converts all characters in the string to uppercase.
                    
    # Example
    text = "hello"
    print(text.upper())  # Output: HELLO
                    
                
  • str.title(): Converts the first character of each word to uppercase.
                    
    # Example
    text = "hello world"
    print(text.title())  # Output: Hello World
                    
                
  • str.capitalize(): Converts the first character of the string to uppercase and the rest to lowercase.
                    
    # Example
    text = "hello"
    print(text.capitalize())  # Output: Hello
                    
                
  • str.strip(): Removes any leading (spaces at the beginning) and trailing (spaces at the end) characters.
                    
    # Example
    text = "  hello  "
    print(text.strip())  # Output: hello
                    
                
  • str.replace(old, new): Replaces a substring within the string with a new substring.
                    
    # Example
    text = "hello world"
    print(text.replace("world", "Python"))  # Output: hello Python
                    
                
  • str.find(sub): Returns the lowest index of the substring if it’s found in the string, otherwise -1.
                    
    # Example
    text = "hello world"
    print(text.find("world"))  # Output: 6
                    
                
  • str.split(): Splits the string into a list where each word is a list item. You can specify a separator.
                    
    # Example
    text = "hello world"
    print(text.split())  # Output: ['hello', 'world']
                    
                
  • str.join(iterable): Joins all elements of an iterable with the string as a separator.
                    
    # Example
    text = ["hello", "world"]
    print(" ".join(text))  # Output: hello world
                    
                
  • str.startswith(prefix): Returns True if the string starts with the specified prefix, otherwise False.
                    
    # Example
    text = "hello world"
    print(text.startswith("hello"))  # Output: True
                    
                
  • str.endswith(suffix): Returns True if the string ends with the specified suffix, otherwise False.
                    
    # Example
    text = "hello world"
    print(text.endswith("world"))  # Output: True
                    
                
  • str.isalnum(): Returns True if all characters in the string are alphanumeric (letters and numbers), otherwise False.
                    
    # Example
    text = "hello123"
    print(text.isalnum())  # Output: True
                    
                
  • str.isdigit(): Returns True if all characters in the string are digits.
                    
    # Example
    text = "12345"
    print(text.isdigit())  # Output: True
                    
                
  • str.isalpha(): Returns True if all characters in the string are alphabetic.
                    
    # Example
    text = "hello"
    print(text.isalpha())  # Output: True
                    
                
  • str.isspace(): Returns True if all characters in the string are whitespace characters.
                    
    # Example
    text = "   "
    print(text.isspace())  # Output: True
                    
                
  • str.isdigit(): Returns True if all characters in the string are digits.
                    
    # Example
    text = "12345"
    print(text.isdigit())  # Output: True