Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Public Finance

Authors: Harvey S. Rosen

5th Edition

025617329X, 978-0256173291

Students also viewed these Databases questions