Skip to main content

Section 9.23 Strings and Lists

Two of the most useful methods on strings involve lists of strings. The split method breaks a string into a list of words. By default, any number of whitespace characters is considered a word boundary.
An optional argument called a delimiter can be used to specify which characters to use as word boundaries. The following example uses the string ai as the delimiter:
Notice that the delimiter doesn’t appear in the result.
The inverse of the split method is join. You choose a desired separator string, (often called the glue) and join the list with the glue between each of the elements.
The list that you glue together (wds in this example) is not modified. Also, you can use empty glue or multi-character strings as glue.

Checkpoint 9.23.1.

    What is printed by the following statements?
    my_name = "Edgar Allan Poe"
    name_list = my_name.split()
    init = ""
    for a_name in name_list:
        init = init + a_name[0]
    print(init)
    
  • Poe
  • Three characters but not the right ones. namelist is the list of names.
  • EdgarAllanPoe
  • Too many characters in this case. There should be a single letter from each name.
  • EAP
  • Yes, split creates a list of the three names. The for loop iterates through the names and creates a string from the first characters.
  • William Shakespeare
  • That does not make any sense.

Checkpoint 9.23.2.

In the United States, dates are formatted as month-day-year. For example, the sixth day of November in the year 2099 would be written as 11-6-2099. In Germany, however, dates are formatted as day.month.year. The same day in Europe is written as 6.11.2099.
Complete this program, which asks the user for a date in US format and uses split and join to convert it to German format.

Checkpoint 9.23.3.

In the preceding program, you may not have done any data validation. If the user entered a bad date like 11-6-2023 (wrong delimiter), 55/45/0, or even something like ant/bee/cat, the program would have dutifully returned 45.55.0 and bee.ant.cat for the last two. (My program crashed for 11-6-2023.) Rewrite the program to validate the input. Consider a date to be valid only when:
  • The list resulting from split on '/' must have exactly three elements.
  • Each part of the date consists of only digits.
  • The year is greater than 1752.
  • The month is between 1 and 12.
  • The day is in the correct range for the month.
For the last criterion, you will need to figure out if a year is a leap year or not. You may want to look at Exercise 4.11.10 for a reminder of how to determine that.
The actual reformatting of the date is a very small part of this program; most of the code goes into validation. Though this might surprise you, it is fairly typical of most programs.