Question: Joining a list of Strings(Python) write a program called join() that has one formal parameter and one optional parameter. The formal parameter is a list

Joining a list of Strings(Python)

write a program called join() that has one formal parameter and one optional parameter.

The formal parameter is a list whose elements are strings. The optional parameter sep has a default value of a single blank space (i.e., sep= ; recall that a blank space is a string). join() returns a single string consisting of the concatenation of all the elements in the list, each separated by sep: >>> title = [Blade, Runner, 2049]

>>> join(title) # Use default value of sep.

Blade Runner 2049

>>> join(title, sep=) # sep is an empty string.

BladeRunner2049

>>> join(title, -*-) # sep is 3 characters.

Blade-*-Runner-*-2049

>>> name = [Instellar]

>>> join(name ) # Make sure it works with a one-element list.

Interstellar

>>> join(name, sep="-*-") # It still works

Interstellar

Think a bit about how to dene join() so that the separator isnt added to the last element! Think about how to implement join() . Hints: You have to initialize an accumulator to an empty string before using a for-loop. Recall how to concatenate strings. All you have to do is add them togethere.g., Ho + Ho + Ho creates the string Ho Ho Ho. range(len(listname)) will return a list of integers as long as the list. What does range(len(listname) - 1) return? Recall that the nal item in a list can be accessed using the index [-1]. Add the last element of the list outside the for-loop.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To write a join function in Python that mimics the behavior described follow these steps to ensure both the formal parameter and the optional sep para... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!