5.7. Strings

5.7.1. Length of strings, string slices

Make a program strlast.py that asks the user to enter her family name, and prints the last letter of the family name.


Question 18: To select letters from a string, one has to use

  1. Round brackets (())

  2. Curley brackets ({})

  3. Angle brackets (<>)

  4. Square brackets ([])

Correct answer: d

Feedback: A correct program is below. Note that an alternative for getting the last item in a list is string[-1].

# program that prints the last letter of your family name

string = input("Enter your family name: ")

length = len(string)

lastLetter = string[length - 1]

print(f"The last letter of your family name is a {lastLetter}")


Write a program strrange.py that asks the user to enter her family name, and prints the 3rd, 4th and 5th letter of the name.


Question 19: What index (on the string containing the family name) could be used for this?

  1. 2:5

  2. 3:5

  3. 4:6

  4. -2:-5

Correct answer: a

Feedback: None

# program that prints the 3rd up to (and including) the 5th letter of your first name

string = input("Enter your family name: ")

print(f"The 3rd, 4th and 5th letter of your family name are {string[2:5]}")


5.7.2. String traversal (part 1)

In this exercise, you will create a program that prints your family name backwards. As a hint, first make a program with a while statement (save it as strback1.py) that prints the following:

10 9 8 7 6 5 4 3 2 1 0

Question 20: By default, a print statement prints a newline after printing the value. How can this be changed to a whitespace (instead of a newline)? By adding to the print statement:

  1. end='\t', for instance print(i, end='\t')

  2. end=' ', for instance print(i, end=' ')

  3. end=, for instance print(i, end=)

Correct answer: b

Feedback: None


With the knowledge gained from the exercise above, write a program that asks for your family name, and prints it backwards. Save it as strback2.py.


Question 21: You will need the number of letters in the family name. How can this be retrieved?

  1. Using len(..), which is in the string module.

  2. Using length(..).

  3. Using another loop.

  4. Using len(..).

Correct answer: d

Feedback: Below one of the possible scripts is given.

# program that prints your name backwards

string = input("Enter your family name: ")

stringBack = ""

i = len(string) - 1
while i >= 0:
    letter = string[i]
    stringBack = stringBack + letter
    i = i - 1

print(f"Your name backwards is {stringBack}")
print(stringBack)


5.7.3. String traversal (part 2), string methods

Run this program:

# program that prints the result of a division
a = 2.0/8.0
print(f"The division directly printed: {a}")

It shows that decimals in floating points are printed using a dot (.). Although this is actually standard in English, some programs read and write decimals using a comma, i.e. 0,25 instead of 0.25.

As an exercise, modify the program given above, such that it prints the result of a division (i.e., the variable a) using a decimal comma. Convert a to a string, and use a string traversal encoded in a while statement (like you did in the previous exercise) to replace the dot with a comma. Save the program as strreplace1.py..


Question 22: Almost certainly you need an if statement inside the while loop. How did you use it?

  1. To stop the loop at the end of the string.

  2. To select letters that are equal to a ..

  3. To rerun the loop in case a . is not found.

Correct answer: b

Feedback: None

# program that prints the result of a division
# using a comma instead of a .
a = 2.0 / 8.0
print(f"The division directly printed: {a}")

aString = str(a)

outString = ""
i = 0
while i < len(aString):
    letter = aString[i]
    if letter == ".":
        outLetter = ","
    else:
        outLetter = letter
    outString = outString + outLetter
    i = i + 1

print(outString)


The previous exercise showed that it is quite a lot of work to manipulate strings. For this reason, Python comes with built-in methods on strings. These methods are called using dot notation, e.g. str.find('apple','a'). Have a look at the description of the built-in str methods in the Python documentation. Now, rewrite strreplace1.py using the appropriate function from str. Save the program as strreplace2.py.


Question 23: Which function did you use?

  1. str.find(..)

  2. str.replace(..)

  3. str.partition(..)

  4. str.split(..)

Correct answer: b

Feedback: None

# program that prints the result of a division
# using a comma instead of a .
a = 2.0 / 8.0
print(f"The division directly printed: {a}")

aString = str(a)

outString = str.replace(aString, ".", ",")

print(outString)