Fix the attributeerror: str object has no attribute append error in Python

Today we’ll look into a common error that you might get in your Python 3 development environment (such as Pycharm, Jupyter, VSCOde, IDLE etc’) when trying to insert text into a string using the append method. It’s key to understand that append is a method of the list object and not relevant to strings. Read on for how we can fix this error.

Using join to solve attribute error no append for strings

Assume you have the following couple of strings that you would like to concatenate

pack_str = "Pandas,Numpy,Seaborn,Matplotlib"
new_elm = 'C++'

Let’s see what happens if we try to simply append a new element into the string:

pack_str.append(new_elm)

Here’s our error:

Fix the attributeerror: str object has no attribute append 

A simple solution is to use the string join method instead:

', '.join([pack_str, new_elm])

This will render the following string:

'Pandas, Numpy, Seaborn, Matplotlib, C++'

How did that work? We passed a list containing all strings we would like to concatenate and defined comma as the separator.

Solve the attribute error by appending to a list

A little bit more busy method that you can use is to split your string into elements in a list, then use the append list method, and then converting the list to a string using join.

Here we go with the code snippet that you can use:

pack_str_lst = pack_str.split(',')
pack_str_lst.append('C++')
', '.join(pack_str_lst)

And the resulting string object , as expected will be:

'Pandas, Numpy, Seaborn, Matplotlib, C++'