Edexcel Computer Science – Python Programming
Practice
Array and String Processing Worksheet
Part A — Questions
Q1 — Create the Array
Write Python code to create a list called words containing:
wind, wire, borrowing, swing, winter, winner, window.
Q2 — User Input
Write Python code that asks the user:
Enter a word or 1 to exit:
Q3 — Exit Condition
Write code that stops the program if the user enters 1.
Q4 — Repeat the Program
Modify the code so the program keeps asking for input until the user enters 1.
Q5 — First Letter
Store the first letter of the input word in a variable called first_letter.
Q6 — Same First Letter
Display all words in the list that start with the same first letter as the input word.
Q7 — Count Same First Letter Words
Display how many words begin with that letter.
Q8 — Words Containing the Input
Display all words that contain the input word.
Q9 — Count Words Containing the Input
Display how many words contain the input word.
Q10 — Longest Word
Find the longest word that contains the input word and display its length.
Q11 — Shortest Word
Find the shortest word that contains the input word and display its length.
Q12 — Full Program
Combine all parts into one program that repeats until the user enters 1.
Part B — Answers
A1 — Array
words = ["wind", "wire", "borrowing", "swing", "winter", "winner", "window"]
A2 — User Input
user_word = input("Enter a word or 1 to exit: ")
A3 — Exit Condition
if user_word == "1":
exit()
A4 — Repeat Program
while True:
user_word = input("Enter a word or 1 to exit: ")
if user_word == "1":
break
A5 — First Letter
first_letter = user_word[0]
A6 — Same First Letter
for w in words:
if w[0] == first_letter:
print(w)
A7 — Count Same Letter
count = 0
for w in words:
if w[0] == first_letter:
print(w)
count += 1
print(count, "word(s) begin with", first_letter)
A8 — Words Containing Input
for w in words:
if user_word in w:
print(w)
A9 — Count Containing
count = 0
for w in words:
if user_word in w:
print(w)
count += 1
print(count, "word(s) contain", user_word)
A10 — Longest Word
longest = ""
for w in words:
if user_word in w:
if len(w) > len(longest):
longest = w
print("The longest word has", len(longest), "letters")
print("The longest word is", longest)
A11 — Shortest Word
shortest = ""
for w in words:
if user_word in w:
if shortest == "" or len(w) < len(shortest):
shortest = w
print("The shortest word has", len(shortest), "letters")
print("The shortest word is", shortest)