FAQ: Learn Python - Lists and Functions - Using strings in lists in functions

This community-built FAQ covers the “Using strings in lists in functions” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise Using strings in lists in functions:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

2 posts were split to a new topic: Adding Spaces Between Concatenated Words

2 posts were split to a new topic: When Are String Indexes Used?

Hi everyone,
Why is it that for this lesson,

for word in words:
result += word

will work, but

for word in words:
word += result

won’t?

thanks!

The += operator in the case of strings,

(1) result += word … Where result and word are strings,

  • … first evaluates result to obtain a value (i.e., find out what string is assigned to it.)
  • Then “adds” (concatenates) the value (assigned string) of word to that value. That means that the value of word is joined to the right side of the string result to produce a new string
  • Then the value of the new string is assigned to the variable result
result = 'abc'
word = 'xyz'

result += word

print(result)

Output:

abcxyz

Now, if in (1) above, you switch result and word , what will happen? Try it!!!

2 Likes

I am having difficulties understanding how this function works. When I wrote it out originally it only drew the first name, probably due to the fact that I was using the term “item” in my iteration instead of the term “word” but I’m unsure of that being the cause or not.

And thank you for explaining how the “result += word” strings work in this function. I kept tripping over it.

Could you post a code sample? I personally suspect your problem may be that you incorrectly indented the return statement since I had the exact same problem. Try deleting two spaces preceding it.
I doubt using result += item is your problem, since after fixing the aforementioned indentation, I passed using result += string.

def join_strings(words):
result=""
for x in words:

result= result+words[x]

return result
print join_strings(n)

why does this code gives error? can I not iterate a list of strings like this using for loop?

You can iterate over a list of strings, just as you would for a list containing any other data type.


Take a closer look at the following code.

for x in words:
  result = result + words[x]

Rather than giving you the answer, here are two questions that will guide you twards figuring out what’s wrong.

What is the value of x each time the loop iterates? If we are using list indices, how are they used?

1 Like