Skip to main content

Section 7.9 Program: Changing the Form of a Name

You now know enough to implement one of the programs we mentioned in Section 7.1: Change a name in the form "Fulano, Juan" to "Juan Fulano". We’ll use a function to do the heavy lifting.

Checkpoint 7.9.1.

Put the steps in the pseudo-code for the function the right order:

Checkpoint 7.9.2.

Write the code in Python.
  • Use the find method to find the index where a comma occurs in the name. If there is no comma, the find method returns -1.
  • Use slices to extract the last name (everything from the beginning up to the comma) and first name (everything after the comma). Remember that the slice goes up to, but not including, the position specified by the number to the right of the :.
  • Use the strip method to strip leading and trailing spaces from a string.
Test your program with names like Fulano, Juan; van Gogh, Vincent; Marie Toulemonde; and Prince.