Python · 2.2
String Manipulation
Name
Class
Date
Mark / 28
Section A
Circle the letter of the correct answer. One mark each.
- 1What does
"Computing"[0:4]give?1- Aomput
- BComp
- CCompu
- Dputing
- 2What does
text.upper()do to the variable text?1- AChanges text to upper case
- BCauses an error
- CDeletes text
- DNothing, it returns a new string
- 3What does
word[::-1]give?1- AThe last character
- BThe first character
- CThe string reversed
- DAn error
- 4What does
"a,b,c".split(",")produce?1- A['a', 'b', 'c']
- B['a,b,c']
- C'abc'
- DAn error
- 5Which method checks that a string contains only digits?1
- Aisnumber()
- Bisdigit()
- Cdigit()
- Disint()
- 6What does
f"Score: {points}"do?1- ACauses an error
- BCreates a list
- CInserts the value of points into the text
- DPrints the word points
- 7What does
"Computing"[-1]give?1- AAn error
- Bg
- C9
- DC
- 8How is join called?1
- AIt is a built in function, not a method
- BOn the first item
- COn the list, as in names.join(",")
- DOn the separator, as in ",".join(names)
- 9What does
"Hello".find("z")return?1- A-1
- BAn error
- CNone
- D0
- 10Why compare
reply.lower() == "yes"rather thanreply == "yes"?1- AIt runs faster
- Blower() removes spaces
- CSo Yes and YES are also accepted
- DStrings cannot be compared directly
Section B
Answer in the space provided. The number of marks tells you how many separate points to make.
- 1StateState what is meant by saying that strings in Python are immutable.2
- 2WriteWrite a program that counts how many vowels are in a word entered by the user.4
- 3ExplainExplain why
name.title()on its own appears to have no effect, and state the correction.3 - 4WriteWrite a program that reads a line of text containing names separated by commas and prints each name on its own line, numbered.5
- 5ExplainExplain how a program can check whether a phrase is a palindrome, ignoring spaces and capital letters.4
Mark scheme
Section A
Section B
1 State what is meant by saying that strings in Python are immutable. 2
One mark for each of the following, to a maximum of 2.
- A string cannot be changed after it is created
- Methods return a new string rather than altering the original
Model answer. Immutable means that once a string has been created its contents cannot be altered. Methods such as upper, replace and strip do not change the original string at all: they build and return a brand new string, which is why the result must be assigned to a variable if it is to be kept.
2 Write a program that counts how many vowels are in a word entered by the user. 4
One mark for each of the following, to a maximum of 4.
- Reads the word and converts it to lower case
- Initialises a counter to zero
- Loops through each character in the word
- Increases the counter when the character is a vowel and prints the total
Model answer. word = input("Enter a word: ").lower() vowels = 0 for letter in word: if letter in "aeiou": vowels += 1 print(f"{word} contains {vowels} vowels") Converting the input to lower case means capital letters are counted too. The loop walks through the string one character at a time, and the in operator checks whether the current character appears in the string of vowels, which is much shorter than writing five separate comparisons.
3 Explain why name.title() on its own appears to have no effect, and state the correction. 3
One mark for each of the following, to a maximum of 3.
- Strings are immutable so the original cannot be changed
- The method builds and returns a new string
- The returned value must be assigned, as in name = name.title()
Model answer. Strings in Python are immutable, so no method is able to alter the string it is called on. The title method builds a brand new string with each word capitalised and returns it, leaving the original completely untouched. Written on a line by itself the returned string is simply discarded, so the variable appears unchanged. The correction is to assign the result back, writing name = name.title(), which makes the name refer to the new string instead.
4 Write a program that reads a line of text containing names separated by commas and prints each name on its own line, numbered. 5
One mark for each of the following, to a maximum of 5.
- Reads the line of input
- Splits it on commas into a list
- Loops through the resulting list
- Removes surrounding spaces from each name
- Prints each name with a number
Model answer. line = input("Enter names separated by commas: ") names = line.split(",") for i, name in enumerate(names, start=1): print(f"{i}. {name.strip()}") The split method breaks the line into a list at every comma. Because the user is likely to type a space after each comma, strip is used to remove any surrounding whitespace from each name before it is printed. Using enumerate with start set to 1 gives a counter beginning at 1 rather than 0, which is more natural for a numbered list shown to a user.
5 Explain how a program can check whether a phrase is a palindrome, ignoring spaces and capital letters. 4
One mark for each of the following, to a maximum of 4.
- Convert the phrase to a single case
- Remove the spaces
- Reverse the phrase using a slice with a step of -1
- Compare the normalised phrase with its reverse
Model answer. The phrase must first be normalised so that differences which should not count are removed. Converting it entirely to lower case means a capital letter at the start does not cause a mismatch, and replacing every space with nothing means the spacing of the original phrase is ignored. The normalised phrase is then reversed using the slice [::-1], which walks through the characters with a step of minus one. If the normalised phrase and its reverse are equal then the phrase reads the same in both directions and is a palindrome. Without normalising first, a genuine palindrome such as Never odd or even would be reported as not being one.