Skip to main content

Section 9.9 Splitting and Joining Strings

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 character(s) 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 (wd_lst in this example) is not modified. Also, you can use empty glue (see line 8 above) or multi-character strings (see line 7 above) as glue.
Check your understanding

Checkpoint 9.9.1.

Create a new list of the 6th through 13th elements of lst (eight items in all) and assign it to the variable output.

Checkpoint 9.9.2.

Create a variable output and assign to it a list whose elements are the words in the string str1.
You have attempted of activities on this page.