IGCSE Python Revision Booklet
String Methods
.strip() - Removes whitespace from start and end
name = " Zara "
print([Link]()) # "Zara"
.split() - Splits a string into a list by spaces or separator
data = "a,b,c"
print([Link](",")) # ['a', 'b', 'c']
.join() - Joins a list into a string
words = ["Hello", "world"]
print(" ".join(words)) # "Hello world"
.lower() / .upper() - Convert string to lower or upper case
print("Ali".lower()) # "ali"
print("Ali".upper()) # "ALI"
List Methods
.append(item) - Adds item to end of list
myList = [1, 2]
[Link](3) # [1, 2, 3]
.pop(index) - Removes item at index
[Link](0) # Removes first item
.sort() / sorted() - Sort list or return sorted copy
nums = [3, 1, 2]
[Link]() # [1, 2, 3]
IGCSE Python Revision Booklet
print(sorted(nums)) # [1, 2, 3]
File Handling
Reading from a file:
with open("[Link]", "r") as file:
for line in file:
print([Link]())
Writing to a file:
with open("[Link]", "w") as file:
[Link]("Hello\n")
Appending to a file:
with open("[Link]", "a") as file:
[Link]("More text\n")
Defensive Programming & Validation
.isdigit() - Check if input is a number
if [Link]():
Try/Except - Handle errors safely
try:
num = int(input("Enter number: "))
except ValueError:
print("Invalid input")
Loop until valid input:
word = input("Enter 2-letter word: ")
IGCSE Python Revision Booklet
while len(word) != 2:
word = input("Try again: ")
Practice Questions
1. Ask the user to input 5 numbers. Save them in a list and display the average.
2. Open a file called "[Link]" and read each line, then print it in uppercase.
3. Validate that a user's input is exactly 2 characters long and alphabetic.
4. Write a program that reads comma-separated numbers from a file and prints the sum for each line.
5. Use .split(), .strip(), and .join() in a mini program that formats names from a file.