Using strings in lists in function


#1



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


It won't append the each word to result.
Traceback (most recent call last):
File "python", line 10, in
File "python", line 6, in join_strings
AttributeError: 'str' object has no attribute 'append'


It should append the words to the list


n = ["Michael", "Lieberman"]
# Add your function here
def join_strings(words):
    result = ""
    for w in range(len(words)):
        result.append(w)
    return result


print join_strings(n)


#2

and gives good reason not to...

Traceback (most recent call last):
  File "python", line 10, in 
  File "python", line 6, in join_strings
AttributeError: 'str' object has no attribute 'append'

It would, if it were a list. This code initializes result as an empty string.


#4

i know i'm the idiot here but i really have no idea what i'm suppose to understand of your answer.


#5

AttributeError: 'str' object has no attribute 'append'.

The String class does not have an .append() method.

result = ""

# result.append(w) will not work

Above, result is initialized to an empty string.

result = []

Above, result is initialized to an empty list.

result.append(w)

will work.


#6

i had the same problem RESULT = "" ((( THIS IS AN EMPTY STRING [""])))
try that see if that helps. I went went back to the lesson on strings for the answer.


#7

I have the same problem people here seem to want to sound smart while not really answering the posted question


#8

You are doing wrong .append is for list only you can append strings using this -
result = ""
for i in range(len(w)):
result += w[i]


#9

Given a list of terms, to join them with white space preserved (spaces between words) then we either have to insert a space after each word, or use the built-in function, .join(). The problem with inserting spaces is that the string ends up with a trailing space that will become evident if we close the string with a full stop.

Michael Lieberman .

Using the built-in function,

return " ".join(words)

This will not have any leading or trailing white space.


#10

This works. Unfortunately, the codecademy directions employ the word append, which is confusing, since the append command is not needed here. Frustrating.


#11

According to the error message that comes up, there is no white space expected in the returned string. String class does not have an .append() method. Given a list of terms, we can as above use either concatenation, or "".join(list). When the SCT tests with ['x','y'','z'] it expects "xyz" on return.


#13