We can use string to list conversions, the split() function and RegEx functions to fix AttributeError: ‘str’ object has no attribute ‘append’ in Python. Follow this article to better understand.
What causes the error “AttributeError: ‘str’ object has no attribute ‘append'” in Python?
The string is a common data type in Python. Strings are also container-type objects like ‘list’; however, you cannot use the ‘append’ method to append an element to the end of a string.
AttributeError is one of the exceptions in Python. AttributeError occurs when you access an undefined attribute on an object.
The error “AttributeError: Object ‘str’ has no attribute ‘append'” occurs when you try to access the attribute ‘append’ on a string note that in the string data structure, there is no attribute ‘append’. The ‘append‘ attribute appears only on list data structures.
Example:
myStr = "visit" myStr = myStr.append(" learnshareit website") print(myStr)
Output:
Traceback (most recent call last):
File "./prog.py", line 2, in <module>
myStr = myStr.append(" learnshareit website")
AttributeError: 'str' object has no attribute 'append'
How to solve this error?
Convert the string to a list
I can show you ways to convert a string to a list. Once you have returned the string to a list, you can call the ‘append’ attribute without AttributeError: ‘str’ object has no attribute ‘append’.
Use list() function
Syntax:
list([object])
Parameters:
- object: can be string, tuple, set, dictionary….
Example:
- Create a string.
- Use the list() function to convert a string to a list.
- Call the append attribute to append an element to the end of the list.
myStr = 'visit' # Convert string to list myList = list(myStr) myList.append('learnshareit website') print(myList)
Output:
['v', 'i', 's', 'i', 't', 'learnshareit website']
Use split() function
Syntax:
str.split(sep, maxsplit)
Parameters:
- sep: the character that splits the original string into a small string. The default is a space.
- maxsplit: maximum number of splits.
Example:
- Create a string.
- Use the split() function to convert a string to a list.
- Call the append() attribute to append an element to the end of the list.
myStr = 'visit' # Convert string to list myList = myStr.split() # The append() attribute to append an element to the end of the list myList.append('learnshareit website') print(myList)
Output:
['visit', 'learnshareit website']
Use RegEx
In Python, Regular Expression is expressed through the ‘re’ module, so you have to import module ‘re’ before you want to use Regular Expression. Module ‘re’ has many methods and functions to work with RegEx, but one of the essential methods is ‘re.sub’.
The Re.sub() method will replace all pattern matches in the string with something else passed in and return the modified string.
Syntax:
re.sub(pattern, replace, string, count)
Parameters:
- pattern: is RegEx.
- replace: is the replacement for the resulting string that matches the pattern.
- string: is the string to match.
- count: is the number of replacements. Python will treat this value as 0, match, and replace all qualified strings if left blank.
Example:
- Import ‘re’ module.
- Call the re.sub() function in conjunction with split() converts the string to a list.
- Call the ‘append’ attribute and add the element to the end of the list.
import re myStr = 'visit' # Use the re.sub() function in conjunction with split() converts the string to a list myList = re.sub('<[^<]+?>','', myStr).split() # The 'append' attribute adds the element to the end of the list myList.append('learnshareit website') print(myList)
Output:
['visit', 'learnshareit website']
Use operator add
You can use the string concatenation operator instead of the ‘append’ attribute because the ‘append’ attribute appears only in the list.
Example:
myStr1 ='visit' myStr2 ='learnshareit' # Use the operator add to connect two strings print(myStr1 + myStr2)
Output:
visitlearnshareit
Summary
If you have any questions about the “AttributeError: ‘str’ object has no attribute ‘append'” error in Python, please leave a comment below. I will answer your questions. Thanks for reading!
Maybe you are interested:
- How To Fix NameError: Name ‘datetime’ Is Not Defined In Python?
- How To Solve AttributeError: ‘Str’ Object Has No Attribute ‘Trim’ In Python
- AttributeError: ‘bytes’ object has no attribute ‘encode’

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java