Back to the topic

The mark scheme starts on a new page, so you can print pages one and two for a student and keep the last page yourself. Everything prints in black on white whichever theme you are using.

Python · 2.2

String Manipulation

Section A: 10 multiple choice questions, 10 marks. Section B: 5 written questions, 18 marks. Total 28 marks.

Name

Class

Date

Mark  / 28

Section A

Circle the letter of the correct answer. One mark each.

  1. 1What does "Computing"[0:4] give?1
    1. Aomput
    2. BComp
    3. CCompu
    4. Dputing
  2. 2What does text.upper() do to the variable text?1
    1. AChanges text to upper case
    2. BCauses an error
    3. CDeletes text
    4. DNothing, it returns a new string
  3. 3What does word[::-1] give?1
    1. AThe last character
    2. BThe first character
    3. CThe string reversed
    4. DAn error
  4. 4What does "a,b,c".split(",") produce?1
    1. A['a', 'b', 'c']
    2. B['a,b,c']
    3. C'abc'
    4. DAn error
  5. 5Which method checks that a string contains only digits?1
    1. Aisnumber()
    2. Bisdigit()
    3. Cdigit()
    4. Disint()
  6. 6What does f"Score: {points}" do?1
    1. ACauses an error
    2. BCreates a list
    3. CInserts the value of points into the text
    4. DPrints the word points
  7. 7What does "Computing"[-1] give?1
    1. AAn error
    2. Bg
    3. C9
    4. DC
  8. 8How is join called?1
    1. AIt is a built in function, not a method
    2. BOn the first item
    3. COn the list, as in names.join(",")
    4. DOn the separator, as in ",".join(names)
  9. 9What does "Hello".find("z") return?1
    1. A-1
    2. BAn error
    3. CNone
    4. D0
  10. 10Why compare reply.lower() == "yes" rather than reply == "yes"?1
    1. AIt runs faster
    2. Blower() removes spaces
    3. CSo Yes and YES are also accepted
    4. DStrings cannot be compared directly

Section B

Answer in the space provided. The number of marks tells you how many separate points to make.

  1. 1StateState what is meant by saying that strings in Python are immutable.2
  2. 2WriteWrite a program that counts how many vowels are in a word entered by the user.4
  3. 3ExplainExplain why name.title() on its own appears to have no effect, and state the correction.3
  4. 4WriteWrite a program that reads a line of text containing names separated by commas and prints each name on its own line, numbered.5
  5. 5ExplainExplain how a program can check whether a phrase is a palindrome, ignoring spaces and capital letters.4

Mark scheme

Section A

1B2D3C4A5B6C7B8D9A10C

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.