0

(HELP) Stuck on last exercise in Python course

Pretty much as the title says. I'm pretty damn close to the end of the course and am right at the last exercise. I've been trying to solve it for over an hour now, and I still don't have a clear idea of what I have to do to get the result I want. As a brief primer on the exercise, I have to write a function that takes multiple words as its argument via the *args parameter and returns a contcatenated version of those words seperated by dashes, such as "hello-world-!". My first thought was to get all the arguments into a single string and then use regex to swap out all the whitespaces for dashes before printing, but I can't seem to actually get the functions into a single string. I just get the functions as a list, dictionary, or tuple. I've tried a variety of tricks that didn't work, and googling wasn't all that helpful either, so I'm turning to you people for help. Am I missing something? Is my entire approach here just wrong and I should be doing something else? Whatever it is, I hope you can help me here. Either way, here is my code so far (the print statement in the function definition is just for development purposes): def concantenate(word, *args): import re pattern = r"(\w+\s+\w)" words = [word, *args] print(words) print(concantenate("I", "love", "Python", "!"))

17th May 2021, 4:51 PM
James Anthony Ville
5 odpowiedzi
+ 5
to create the new string you can also use: "-".join(<your list of strings>) BTW, can you please name the python tutorial ( there are several different), and also the exercise you are talking about thanks!
17th May 2021, 5:11 PM
Lothar
Lothar - avatar
+ 5
Aysha , sorry to say, but why a hard- coded string is used to get the result in the concatenate() function. we have a list of all strings in args parameter, so we can use this together with the join() function.
17th May 2021, 5:32 PM
Lothar
Lothar - avatar
+ 1
UPDATE: It's done. After fiddling around with the join() function a bit, I've actually managed to get some code that does what I need it do. I could have made it a bit more compact, but it does the job. def concantenate(*args) dash = "-" words = (args) return(dash.join(words)) print(concatenate("I", "love", "Python", "!")) Still, I'm finally done with the "Learn Python Core" course. Couldn't have really done it without some help from the discussion forums here, but I got there in the end. Where to next?
19th May 2021, 3:55 PM
James Anthony Ville