16 Using Strings in lists in functions


#1



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

= ["Michael", "Lieberman"]

Add your function here

def join_strings(words):
result = ""
for item in words:
result = result + item
return result
print join_strings(n)

This is my code. It works but I'm not sure what it does. It gives the result "MichaelLieberman", but I'm not sure how adding item to result creates that. Can anyone explain how the code seems to know that "Michael" and "Lieberman" has to go between the two quotation marks in result? Thanks.


#2

n = ["Michael", "Lieberman"]#simple list
# Add your function here
def join_strings(words): #creating a function that takes words as parameter
    result = "" #make an empty string under variable "results"
    for word in words: #for each object in our parameter "words"
        result= result + word #add the object to the "results" string
    return result # return the "results" value to show finished product

print join_strings(n) #print to console

Hope that explains it for you. Message me if you still need more help. :slight_smile:


#3

Hi,
thank you for the answer.
I am a bit confused on using the word append. This was my code:
n = ["Michael", "Lieberman"]

Add your function here

def join_strings(words):
result = ""
for i in range(len(words)):
result.append(i) """ can anyone please explain why this is wrong?
return result
print join_strings(n)

Many thanks


#4

Hi, @arwaahmed ,

It is important to format code when you post it. That enables users to see your indentation and other important details.

See How do I format code in my posts?.

You have chosen to begin with an empty string, which you assign to result, here ...

result = ""

... and then to accumulate the data there.

The append method can be called from lists, but not from strings, therefore this is invalid ...

result.append(i)

Having committed to use a string as an accumulator, you should use concatenation, instead, to accumulate the characters.

Also, since you are iterating through indexes, using i, here ...

for i in range(len(words)):

... you must use that index, i, to access individual characters in the string that is referenced by words. You are attempting to append the index, i, directly to result.

Modifying your statement to use indexes and concatenation, you can do this ...

result = result + words[i]

... or this ...

result += words[i]

#5

Thank you so much. I will format code in the future, it looks so tidy.!


#6

A post was split to a new topic: It works on IDLE


#7

I used the following code:

n = ["Michael", "Lieberman"]

def join_strings(words):
    result = ""
    for i in range(len(words)):
        result = result + words[i]
        return result

print join_strings(n)

But am getting the error:
Oops, try again. join_strings(['x', 'y', 'z', 'a']) returned 'x' instead of 'xyza'

Why might that be?


#8

Hi, @arccoder10295 ,

Indentation defines the structure of a Python program. Was it your intention to make the return statement part of the for loop?


#9

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