A user submits a search query: " PIZZA ". Before searching, you need to clean it up (strip spaces), normalize it (lowercase), then search your database. String operations make this possible.

Change case (normalize input)

Convert text to uppercase or lowercase for consistent comparisons.

case.py
text = 

upper = text.upper()
lower = text.lower()
title = text.title()
capitalize = text.capitalize()

print(f"Original:   {text}")
print(f"Upper:      {upper}")
print(f"Lower:      {lower}")
print(f"Title:      {title}")
print(f"Capitalize: {capitalize}")

# Practical: case-insensitive comparison
user_answer = 
if user_answer.lower() == "yes":
    print("User agreed (any case)")
text = 

upper = text.upper()
lower = text.lower()
title = text.title()
capitalize = text.capitalize()

print(f"Original:   {text}")
print(f"Upper:      {upper}")
print(f"Lower:      {lower}")
print(f"Title:      {title}")
print(f"Capitalize: {capitalize}")

# Practical: case-insensitive comparison
user_answer = 
if user_answer.lower() == "yes":
    print("User agreed (any case)")
text = 

upper = text.upper()
lower = text.lower()
title = text.title()
capitalize = text.capitalize()

print(f"Original:   {text}")
print(f"Upper:      {upper}")
print(f"Lower:      {lower}")
print(f"Title:      {title}")
print(f"Capitalize: {capitalize}")

# Practical: case-insensitive comparison
user_answer = 
if user_answer.lower() == "yes":
    print("User agreed (any case)")
text = 

upper = text.upper()
lower = text.lower()
title = text.title()
capitalize = text.capitalize()

print(f"Original:   {text}")
print(f"Upper:      {upper}")
print(f"Lower:      {lower}")
print(f"Title:      {title}")
print(f"Capitalize: {capitalize}")

# Practical: case-insensitive comparison
user_answer = 
if user_answer.lower() == "yes":
    print("User agreed (any case)")
text = 

upper = text.upper()
lower = text.lower()
title = text.title()
capitalize = text.capitalize()

print(f"Original:   {text}")
print(f"Upper:      {upper}")
print(f"Lower:      {lower}")
print(f"Title:      {title}")
print(f"Capitalize: {capitalize}")

# Practical: case-insensitive comparison
user_answer = 
if user_answer.lower() == "yes":
    print("User agreed (any case)")
text = 

upper = text.upper()
lower = text.lower()
title = text.title()
capitalize = text.capitalize()

print(f"Original:   {text}")
print(f"Upper:      {upper}")
print(f"Lower:      {lower}")
print(f"Title:      {title}")
print(f"Capitalize: {capitalize}")

# Practical: case-insensitive comparison
user_answer = 
if user_answer.lower() == "yes":
    print("User agreed (any case)")
text = 

upper = text.upper()
lower = text.lower()
title = text.title()
capitalize = text.capitalize()

print(f"Original:   {text}")
print(f"Upper:      {upper}")
print(f"Lower:      {lower}")
print(f"Title:      {title}")
print(f"Capitalize: {capitalize}")

# Practical: case-insensitive comparison
user_answer = 
if user_answer.lower() == "yes":
    print("User agreed (any case)")
text = 

upper = text.upper()
lower = text.lower()
title = text.title()
capitalize = text.capitalize()

print(f"Original:   {text}")
print(f"Upper:      {upper}")
print(f"Lower:      {lower}")
print(f"Title:      {title}")
print(f"Capitalize: {capitalize}")

# Practical: case-insensitive comparison
user_answer = 
if user_answer.lower() == "yes":
    print("User agreed (any case)")
text = 

upper = text.upper()
lower = text.lower()
title = text.title()
capitalize = text.capitalize()

print(f"Original:   {text}")
print(f"Upper:      {upper}")
print(f"Lower:      {lower}")
print(f"Title:      {title}")
print(f"Capitalize: {capitalize}")

# Practical: case-insensitive comparison
user_answer = 
if user_answer.lower() == "yes":
    print("User agreed (any case)")

Normalize input before comparing - users might type "YES", "Yes", or "yes".

upper/lower Change case: `"Hello".upper()` → `"HELLO"`, `"Hello".lower()` → `"hello"`

Strip whitespace (clean input)

Remove extra spaces from the beginning and end of user input.

strip.py
messy_input = "   Alice Smith   "
clean = messy_input.strip()

print(f"With spaces: '{messy_input}'")
print(f"Stripped:    '{clean}'")
print(f"Length before: {len(messy_input)}")
print(f"Length after:  {len(clean)}")

# Different strip variants
tabbed = 
print(f"strip():  '{tabbed.strip()}'")
print(f"lstrip(): '{tabbed.lstrip()}'")  # left only
print(f"rstrip(): '{tabbed.rstrip()}'")  # right only

# Strip specific characters
dashes = "---hello---"
print(f"strip('-'): '{dashes.strip('-')}'")
messy_input = "   Alice Smith   "
clean = messy_input.strip()

print(f"With spaces: '{messy_input}'")
print(f"Stripped:    '{clean}'")
print(f"Length before: {len(messy_input)}")
print(f"Length after:  {len(clean)}")

# Different strip variants
tabbed = 
print(f"strip():  '{tabbed.strip()}'")
print(f"lstrip(): '{tabbed.lstrip()}'")  # left only
print(f"rstrip(): '{tabbed.rstrip()}'")  # right only

# Strip specific characters
dashes = "---hello---"
print(f"strip('-'): '{dashes.strip('-')}'")

Users often accidentally add spaces. Always strip input before processing.

strip Remove whitespace: `" hi ".strip()` → `"hi"`

Find substring (search in text)

Check if a string contains another string, and where.

find.py
text = 

# Simple check with 'in'
print(f"'Hello' in text? {'Hello' in text}")
print(f"'Java' in text? {'Java' in text}")

# Find position
pos = text.find("Hello")
print(f"'Hello' first found at: {pos}")

# Find from a position
second_pos = text.find("Hello", pos + 1)
print(f"'Hello' second found at: {second_pos}")

# find() returns -1 if not found (doesn't raise error)
not_found = text.find("Java")
print(f"'Java' found at: {not_found}")

# index() raises error if not found - use when you expect it to exist
# pos = text.index("Java")  # Would raise ValueError
text = 

# Simple check with 'in'
print(f"'Hello' in text? {'Hello' in text}")
print(f"'Java' in text? {'Java' in text}")

# Find position
pos = text.find("Hello")
print(f"'Hello' first found at: {pos}")

# Find from a position
second_pos = text.find("Hello", pos + 1)
print(f"'Hello' second found at: {second_pos}")

# find() returns -1 if not found (doesn't raise error)
not_found = text.find("Java")
print(f"'Java' found at: {not_found}")

# index() raises error if not found - use when you expect it to exist
# pos = text.index("Java")  # Would raise ValueError

Use in for simple checks, find() when you need the position.

find Find position: `"Hello".find("l")` → `2`, returns `-1` if not found

Replace text

Substitute parts of a string with something else.

replace.py
original = "Hello World"

# Replace all occurrences
replaced = original.replace("l", "L")
print(f"Replace l→L: {replaced}")

# Replace substring
new_world = original.replace("World", "Python")
print(f"Replace World→Python: {new_world}")

# Original unchanged (strings are immutable)
print(f"Original still: {original}")

# Limit replacements
text = "a-b-c-d-e"
partial = text.replace("-", "_", 2)  # only first 2
print(f"Replace first 2: {partial}")

# Practical: censor words
message = 
censored = message.replace("bad", "***")
print(f"Censored: {censored}")
original = "Hello World"

# Replace all occurrences
replaced = original.replace("l", "L")
print(f"Replace l→L: {replaced}")

# Replace substring
new_world = original.replace("World", "Python")
print(f"Replace World→Python: {new_world}")

# Original unchanged (strings are immutable)
print(f"Original still: {original}")

# Limit replacements
text = "a-b-c-d-e"
partial = text.replace("-", "_", 2)  # only first 2
print(f"Replace first 2: {partial}")

# Practical: censor words
message = 
censored = message.replace("bad", "***")
print(f"Censored: {censored}")

replace creates a new string - the original is unchanged (strings are immutable).

replace Substitute text: `"Hello".replace("l", "L")` → `"HeLLo"`

Split string (parse CSV)

Break a string into parts using a delimiter.

split.py
# Split CSV line
csv_line = 
parts = csv_line.split(",")

print(f"CSV: {csv_line}")
print(f"Name: {parts[0]}")
print(f"Age: {parts[1]}")
print(f"Job: {parts[2]}")

# Split by whitespace (default)
sentence = "Hello   World   Python"
words = sentence.split()
print(f"Words: {len(words)}")
for word in words:
    print(f"  - {word}")

# Limit splits
data = "a:b:c:d:e"
first_two = data.split(":", 2)  # split at most 2 times
print(f"Limited split: {first_two}")

# Split CSV line
csv_line = 
parts = csv_line.split(",")

print(f"CSV: {csv_line}")
print(f"Name: {parts[0]}")
print(f"Age: {parts[1]}")
print(f"Job: {parts[2]}")

# Split by whitespace (default)
sentence = "Hello   World   Python"
words = sentence.split()
print(f"Words: {len(words)}")
for word in words:
    print(f"  - {word}")

# Limit splits
data = "a:b:c:d:e"
first_two = data.split(":", 2)  # split at most 2 times
print(f"Limited split: {first_two}")

Splitting is essential for parsing structured data like CSV files.

split Break apart: `"a,b,c".split(",")` → `["a", "b", "c"]`

Exercise: more_ops.py

Explore more operations: slicing, startswith, endswith, join