91.0k views
2 votes
g Write a function called chain_words(str) which takes in one parameter which is a string that holds multiple words separated by spaces. Your function should return a string with the words chained together with an - (hyphen). You can first split the words at spaces and then join the words with - (hyphen). You can achieve this by using split and join methods respectively.

1 Answer

6 votes

Answer:

Step-by-step explanation:

The following is written in Python. The function takes in a string as a parameter. It then sperates the string at every space. Then it rejoins the list of strings with hyphens. Finally, returning the newly created string with hyphens.

def chain_words(str):

string_split = str.split(" ")

seperator = '-'

hyphen_string = seperator.join(string_split)

return hyphen_string

g Write a function called chain_words(str) which takes in one parameter which is a-example-1
User Mxpv
by
5.4k points