List & String Methods
Python provides powerful built-in methods for manipulating text (strings) and collections (lists). Mastering these tools allows you to write cleaner and more efficient code.
Learning Objectives
- 11.2.3.3 Apply functions and methods of processing lists
- 11.2.2.3 Apply functions and methods of processing strings
Conceptual Anchor
The Power Tools Analogy
If basic operations are hand tools, methods are power tools.
split() is a saw (cutting a log into planks), join() is glue, and
sort() organizes your workshop instantly.
Rules & Theory
String Methods (11.2.2.3)
# Changing Case
s = "Hello World"
print(s.upper()) # "HELLO WORLD"
print(s.lower()) # "hello world"
print(s.title()) # "Hello World"
print(s.swapcase()) # "hELLO wORLD"
# Searching & Replacing
print(s.find("World")) # 6 (index where it starts)
print(s.replace("World", "Python")) # "Hello Python"
print(s.count("l")) # 3
# Checking Content
print("123".isdigit()) # True
print("abc".isalpha()) # True
print(" ".isspace()) # True
# Splitting & Joining
words = s.split() # ['Hello', 'World']
print("-".join(words)) # "Hello-World"
# Stripping Whitespace
raw = " user input "
print(raw.strip()) # "user input"List Methods (11.2.3.3)
lst = [3, 1, 4, 1, 5]
# Adding Elements
lst.append(9) # [3, 1, 4, 1, 5, 9] (add to end)
lst.insert(0, 2) # [2, 3, 1, 4, 1, 5, 9] (insert at index 0)
lst.extend([6, 7]) # [2, 3, 1, 4, 1, 5, 9, 6, 7] (add another list)
# Removing Elements
lst.pop() # Removes last element (7)
lst.pop(0) # Removes element at index 0 (2)
lst.remove(4) # Removes first occurrence of value 4
# lst.clear() # Removes all elements
# Ordering
lst.sort() # Sorts in-place: [1, 1, 3, 5, 6, 9]
print(lst)
lst.reverse() # Reverses in-place: [9, 6, 5, 3, 1, 1]
# Information
print(lst.count(1)) # 2
print(lst.index(9)) # 0 (index of first 9)In-place vs Return
String methods return a new string (strings are immutable). List methods like
sort() change the list in-place and return None.
Worked Examples
1 Palindrome Checker
def is_palindrome(text):
# Remove spaces and convert to lower case
clean_text = text.replace(" ", "").lower()
# Compare with reverse
return clean_text == clean_text[::-1]
print(is_palindrome("Race car")) # True
print(is_palindrome("Hello")) # False2 Processing a List of Names
names = [" ali ", "DANA", "marat "]
cleaned_names = []
for name in names:
cleaned_names.append(name.strip().title())
cleaned_names.sort()
print(cleaned_names) # ['Ali', 'Dana', 'Marat']Pitfalls & Common Errors
Sorting Mistake
x = lst.sort() sets x to None! Determine if you want to
modify the list (lst.sort()) or create a new sorted list
(x = sorted(lst)).
String Immutability
s.upper() does NOT change s. You must assign it:
s = s.upper().
Graded Tasks
What method adds a single element to the end of a list?
Explain why string.replace("a", "b") doesn't verify until you print or assign
it.
Write a program that takes a sentence, splits it into words, sorts them alphabetically, and joins them with hyphens.
Create a password validator that checks if a string has at least one uppercase letter, one digit, and no spaces, using string methods.