Python String Methods

Master text manipulation with essential string methods

📝 Working with Strings

Strings are sequences of characters. Python provides many built-in methods to manipulate and work with text data efficiently.


# Basic string operations
text = "Hello World"
print(text.upper())    # HELLO WORLD
print(text.lower())    # hello world
print(text.split())    # ['Hello', 'World']
                                    
40+
String Methods
Immutable
Cannot Change
Unicode
Support

String Method Categories

🔤

Case Methods

Change text capitalization

upper() lower() title() capitalize()
🔍

Search Methods

Find and locate text

find() index() count() startswith()
✂️

Split & Join

Break apart and combine text

split() join() strip()
🔄

Replace Methods

Modify and substitute text

replace() translate()

🔹 Case Conversion Methods

upper() & lower() - Change Case

Convert text to uppercase or lowercase


text = "Hello World"
print(text.upper())  # HELLO WORLD
print(text.lower())  # hello world

# Useful for comparisons
name1 = "ALICE"
name2 = "alice"
print(name1.lower() == name2.lower())  # True
                            

title() & capitalize() - Format Text

Format text with proper capitalization


text = "hello world python"
print(text.title())      # Hello World Python
print(text.capitalize()) # Hello world python

# Good for names
name = "john doe"
print(name.title())      # John Doe
                            

🔹 Search and Find Methods

find() - Find Position

Returns the position of substring (returns -1 if not found)


text = "Python is awesome"
pos = text.find("is")
print(pos)  # 7

# Not found returns -1
pos = text.find("Java")
print(pos)  # -1
                            

count() - Count Occurrences

Counts how many times a substring appears


text = "banana"
count = text.count("a")
print(count)  # 3

sentence = "The cat in the hat"
count = sentence.count("the")
print(count)  # 1 (case sensitive)
                            

startswith() & endswith() - Check Beginnings/Endings

Check if string starts or ends with specific text


filename = "document.pdf"
print(filename.startswith("doc"))  # True
print(filename.endswith(".pdf"))   # True

# Useful for file processing
if filename.endswith(".txt"):
    print("Text file")
elif filename.endswith(".pdf"):
    print("PDF file")  # This will print
                            

🔹 Split and Join Methods

split() - Break Text Apart

Splits string into a list of words


sentence = "Python is fun"
words = sentence.split()
print(words)  # ['Python', 'is', 'fun']

# Split by specific character
data = "apple,banana,orange"
fruits = data.split(",")
print(fruits)  # ['apple', 'banana', 'orange']
                            

join() - Combine Text

Joins list of strings into one string


words = ["Python", "is", "awesome"]
sentence = " ".join(words)
print(sentence)  # Python is awesome

# Join with different separator
fruits = ["apple", "banana", "orange"]
result = ", ".join(fruits)
print(result)  # apple, banana, orange
                            

strip() - Remove Whitespace

Removes spaces from beginning and end


text = "  Hello World  "
clean = text.strip()
print(f"'{clean}'")  # 'Hello World'

# Remove specific characters
text = "...Hello..."
clean = text.strip(".")
print(clean)  # Hello
                            

🔹 Replace and Modify Methods

replace() - Substitute Text

Replaces all occurrences of a substring


text = "I love Java programming"
new_text = text.replace("Java", "Python")
print(new_text)  # I love Python programming

# Replace multiple occurrences
text = "red red rose"
new_text = text.replace("red", "blue")
print(new_text)  # blue blue rose

# Limit replacements
text = "red red red rose"
new_text = text.replace("red", "blue", 2)  # Replace only first 2
print(new_text)  # blue blue red rose
                            

🔹 Text Checking Methods

isdigit() & isalpha() - Check Content Type

Check if string contains only digits or letters


# Check if all digits
age = "25"
print(age.isdigit())  # True

name = "Alice"
print(name.isalpha())  # True

# Mixed content
mixed = "abc123"
print(mixed.isdigit())  # False
print(mixed.isalpha())  # False
print(mixed.isalnum())  # True (alphanumeric)
                            

🧠 Test Your Knowledge

Which method converts text to uppercase?