Bug With String and Functions


I don't know if this is just me being stupid, but this seems like a bug

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)

And it tells me this: Oops, try again.
join_strings(['x', 'y', 'z', 'a']) returned ' xyza' instead of 'xyza'
If this ain't a bug and just my code is wrong, please tell me how to fix it


No bug In the exercise but the code has bug, while defining the empty string,you defined a string having one space character


How exactly did I do that?


Have you defined result string with a single space character ?
You can see its not empty but it has a length of 1.

If yes and you don't want to change it ,Then slice is one method to get rid of it from final string.


Effectively, you can define it as an empty string. (Then no need of slicing)


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