16/18 Using String Lists in Functions


#1

May I ask, what does += mean? Sorry got lost in the woods. I keep seeing this in the forums and seems to always work like a concatenating function?


16/18 Using String Lists in Functions
#2

Hi, += is the shorthand assignment operator. If you type a += b, it means a = a + b. Similarly we have -=, *= and %= :slight_smile:


#3

n = ["Michael", "Lieberman"]

def join_strings(words):
    result = " "
    for i in range(len(words)):
        result += words[i]
    return result# Add your function here

print join_strings(n)

I saw error as

Oops, try again.
join_strings(['x', 'y', 'z', 'a']) returned ' xyza' instead of 'xyza'

#4

Hi there - this bit's the problem:

  1. Inside the function, create a variable called result and set it to "", an empty string.

It looks like when you're creating the variable result it has a space in the string.

Because of this, the space appears before the name. Apart from that it looks good. Get rid of the space in the string and you should be okay.


#5

Hi blogninja25344,

I am facing issues with the following code where I have remove the space between the "" , but I am getting only the first string printed. The code and the outputs are added below:

Code:
n = ["Michael", "Lieberman"]

Add your function here

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

print join_strings(n)

Output:
Michael
None

Oops, try again.
join_strings(['x', 'y', 'z', 'a']) returned 'x' instead of 'xyza'