In Python, String Methods are built-in functions that allow you to manipulate, analyze, and transform text. Because strings are immutable, these methods do not change the original string; instead, they return a new string with the modifications applied.
String methods can generally be grouped into four categories:
Case Conversion: Methods that change the capitalization of the text.
Search and Find: Methods used to locate characters or substrings.
Cleaning & Stripping: Methods used to remove whitespace or unwanted characters.
Transformation: Methods that split, join, or replace parts of the string.
The syntax for calling a string method is the "dot notation":
string_variable.method_name(arguments)
These are useful for normalizing user input (e.g., converting "YES" and "yes" to the same format).
text = "python is fun" print(text.upper()) # "PYTHON IS FUN" print(text.capitalize()) # "Python is fun" print(text.title()) # "Python Is Fun" mixed = "PyThOn" print(mixed.lower()) # "python"
These methods return either the position of a substring or a True/False value.
msg = "Welcome to EMBLAb" # Check start/end print(msg.startswith("Welcome")) # True print(msg.endswith("Admin")) # False # Finding position print(msg.find("to")) # 8 (Index where "to" starts) print(msg.count("e")) # 2 (How many times 'e' appears) # Content Validation print("12345".isdigit()) # True print("Python".isalpha()) # True (only letters)
Crucial for data cleaning, especially when dealing with files or user forms.
# Removing whitespace user_input = " admin_user " print(user_input.strip()) # "admin_user" # Replacing text sentence = "I like Java" new_sentence = sentence.replace("Java", "Python") print(new_sentence) # "I like Python" # Splitting into a list data = "apple,banana,cherry" fruits = data.split(",") print(fruits) # ['apple', 'banana', 'cherry'] # Joining a list into a string new_data = "-".join(fruits) print(new_data) # "apple-banana-cherry"
| Method | Description | Example |
| .strip() | Removes leading/trailing whitespace. | " hi ".strip() $\rightarrow$ "hi" |
| .lower() | Converts all characters to lowercase. | "Hi".lower() $\rightarrow$ "hi" |
| .replace() | Replaces a substring with another. | "abc".replace("a", "z") $\rightarrow$ "zbc" |
| .split() | Splits string into a list based on a delimiter. | "a b".split() $\rightarrow$ ['a', 'b'] |
| .join() | Merges a list into a single string. | "-".join(["a", "b"]) $\rightarrow$ "a-b" |
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION