String Methods

Overview

String methods allow you to perform specific operations on string data. These are accessed using the dot . operator on string objects.

Examples

# Example 1: upper() - Convert to uppercase
msg = "hello world"
print(msg.upper())         # HELLO WORLD
# Example 2: lower() - Convert to lowercase
msg = "Python IS Fun"
print(msg.lower())         # python is fun
# Example 3: strip() - Remove leading/trailing whitespace
dirty = "   clean me   "
print(dirty.strip())       # "clean me"
# Example 4: replace() - Replace a substring
text = "I love Java"
print(text.replace("Java", "Python"))  # I love Python
# Example 5: split() - Split the string into a list
csv = "apple,banana,grape"
fruits = csv.split(",")
print(fruits)              # ['apple', 'banana', 'grape']