Why can’t I make an "append()" on a list formed from a "Join()"?

Asked

Viewed 410 times

2

I started creating a list in Python leaving each element below the other through the join(). Then I tried to add an element to the list through append():

abc = '\n'.join(['a','b', 'c'])
abc.append('d')

However, when I run the code the following error appears:

Attributeerror: str Object has no attribute 'append'

Why does this happen?

  • If I’m not mistaken append serves to add an item to a list, but the function join returns a string and not a list, so the method does not exist

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

3

The error is precisely this that is written in the error message, interpreting it already knows what occurs.

In case you’re creating a string and guarding in abc, there calls a method called append() in that object, which is still a string, and if you go see the documentation of the type string you will see that he does not possess this method append(), so you can’t call him. You have to call only one of the existing according to the documentation, could only concatenate the letter there, as already demonstrated know how to do since you did in the previous line.

Maybe I wanted to do something else, but the answer to the question is this, because we don’t even know what that other thing would be.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.