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 To select letters from a string, one has to use

  1. Round brackets (())

  2. Curley brackets ({})

  3. Angle brackets (<>)

  4. Square brackets ([])

Correct answers: 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("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 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 answers: a.

Feedback:

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

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

print("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 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 answers: b.

Feedback:

# program that prints 10 9 8 .... 0 
i = 10
while i >= 0:
  # print the value
  # by default, a new line is printed after the print,
  # but this can be changed with end, here it prints a whitespace
  # after printing the variable 
  print(i, end=' ')
  i=i-1

print()

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 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 answers: 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("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("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 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 answers: b.

Feedback:

# program that prints the result of a division
# using a comma instead of a .
a = 2.0/8.0
print("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 Which function did you use?

  1. str.find(..)

  2. str.replace(..)

  3. str.partition(..)

  4. str.split(..)

Correct answers: b.

Feedback:

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

aString = str(a)

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

print(outString)