GEOG 485:
GIS Programming and Software Development

Lesson 2 Practice Exercise 2 Solution



beatles = ["John Lennon", "Paul McCartney", "Ringo Starr", "George Harrison"]

for member in beatles:
    space = member.index(" ")
    last = member[space+1:]
    first = member[:space]
    print (last + ", " + first)
    # or doing away with the last and first variables
    # print (member[space+1:] + ", " + member[:space])


Building on the first exercise, this script also uses a for loop to process each item in the beatles list. And the index method is again used to determine the position of spaces in the names. The script then obtains all characters after the space (member[space+1:]) and stores them in a variable called last. Likewise, it obtains all characters before the space (member[:space]) and stores them in a variable called first. Again, string concatenation is used to append the first name to the last name with a comma in between.