Question: Describe the relationship between string.join(string.split(song)) and
ID: 3619697 • Letter: Q
Question
Question: Describe the relationship between string.join(string.split(song)) and song. Are they the same for all strings? When would they be different?--
The reading for this question is below.
---
Strings and lists:
Two of the most useful functions in the string module involve lists of strings.
The split function breaks a string into a list of words. By default, any number
of whitespace characters is considered a word boundary:
>>> import string
>>> song = "The rain in Spain..."
>>> string.split(song)
[’The’, ’rain’, ’in’, ’Spain...’]
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:
>>> string.split(song, ’ai’)
[’The r’, ’n in Sp’, ’n...’] Notice that the delimiter doesn’t appear in the list.
The join function is the inverse of split. It takes a list of strings and concatenates
the elements with a space between each pair:
>>> list = [’The’, ’rain’, ’in’, ’Spain...’]
>>> string.join(list)
’The rain in Spain...’96 Lists
Like split, join takes an optional delimiter that is inserted between elements:
>>> string.join(list, ’_’)
’The_rain_in_Spain...’ Question: Describe the relationship between string.join(string.split(song)) and song. Are they the same for all strings? When would they be different?
--
The reading for this question is below.
---
Strings and lists:
Two of the most useful functions in the string module involve lists of strings.
The split function breaks a string into a list of words. By default, any number
of whitespace characters is considered a word boundary:
>>> import string
>>> song = "The rain in Spain..."
>>> string.split(song)
[’The’, ’rain’, ’in’, ’Spain...’]
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:
>>> string.split(song, ’ai’)
[’The r’, ’n in Sp’, ’n...’] Notice that the delimiter doesn’t appear in the list.
The join function is the inverse of split. It takes a list of strings and concatenates
the elements with a space between each pair:
>>> list = [’The’, ’rain’, ’in’, ’Spain...’]
>>> string.join(list)
’The rain in Spain...’96 Lists
Like split, join takes an optional delimiter that is inserted between elements:
>>> string.join(list, ’_’)
’The_rain_in_Spain...’
Explanation / Answer
join( words[, sep]) Concatenate a list or tuple of words with intervening occurrences of sep. The default value for sep is a single space character. It is always true that "string.join(string.split(s, sep), sep)" equals s.That is string.join(string.split(song)) and song are equal.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.