Home » AttributeError: ‘str’ object has no attribute ‘append’

AttributeError: ‘str’ object has no attribute ‘append’

To solve AttributeError: ‘str’ object has no attribute ‘append’ error follow below methods. The append() method is used to attach items to the end of a list. It is not possible to use it to add items to a string. If you try to attach values to the end of a string using append, Python gives an error stating “AttributeError:’str’ object has no attribute ‘append'” ().

ERROR LOG

AttributeError: ‘str’ object has no attribute ‘append’

How to solve AttributeError: ‘str’ object has no attribute ‘append’ ?

Refer the given methods to solve the issue.

Method 1:

We attempted to append each name to the end of our “s names” string using append(). Because add() does not work on strings, an error is produced.

We can correct our code by appending a name to the end of the “s names” string with the concatenation operator. Let us change our code to utilise the concatenation operator rather than append():

for n in names:
	if n.startswith("S"):
		s_names = s_names + n + " "

Hope the above solution works.

Also read :

Strange SQLAlchemy error message: TypeError: ‘dict’ object does not support indexing