16. How can we print 2 names using a join? ie) Michael, Lieberman


#1

My code works, I know how to pass the lesson doing both ways, but i would like to know how to use a join so we can separate the names to print two different names so that it Appears --> Michael, Lieberman.

When I use my join it prints M, i, c, h, a, e, lL, i, e, b, e, r, m, a, n which just looks weird.

Any suggestions how I can get it to print Michael, Lieberman?


https://www.codecademy.com/en/courses/python-beginner-nzzVa/3/5?curriculum_id=4f89dab3d788890003000096


n = ["Michael", "Lieberman"]
def join_strings(words):
    result=""
    for i in range(len(words)):
        result += ", ".join(words[i])
    return result

print join_strings(n)


#2

You are on the right track, in fact the only line posing an error would be this one,

Try appending like this,'

result.append(i)

#3

@bandit Thanks for your response. When reading your post at first I thought I would fix this in 2 seconds but it's still not accepting my answer. I still can't wrap my head around it after trying several variations. Would you be able to provide the code?

Thanks


#4

can I see your updated code please


#5

I figured it out. Was over-thinking this. Sometimes the simplest answer is the best.

Final code below...

n = ["Michael", "Lieberman"]

def join_strings(words):
    return (', ').join(words)

print join_strings(n)

will print Michael, Lieberman

PS. This code won't pass the lesson, but wanted to take this lesson one step further


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.