AttributeError: ‘str’ object has no attribute ‘append’
In this guide, we talk about AttributeError: 'str' object has no attribute 'append'
and why it is raised. here use The append() method does not work if you want to add a string to another string because append() is only supported by list items. Python returns an error stating AttributeError: ‘str’ object has no attribute ‘append’.
The AttributeError: ‘str’ object has no attribute ‘append’ error is raised when developers use append() instead of the concatenation operator. You forget to add value to a string instead of a list.
Example
names = ["Ram", "Shyam", "Joi", "Raju"] s_names = "" for n in names: s_names.append(n) Traceback (most recent call last): File "", line 2, in AttributeError: 'str' object has no attribute 'append'
The Solution
names = ["Ram", "Shyam", "Joi", "Raju"] s_names = [] for value in names: s_names.append(value) print("The students names : {}".format(s_names)) Output : The students names : ['Ram', 'Shyam', 'Joi', 'Raju']