How to append a string to a list in Python

Append is one of the most essential operations for any data structure, and lists are no exception. This operation adds an element at the end of the list.

Updated November 3, 2023

In python, lists can be extended (appended) by using the append() method or by using “+=” operator. Here’s how to do it:

# Using append() method
list1 = ['a', 'b', 'c']
list1.append('d')  # Now list1 is ['a', 'b', 'c', 'd'].

# Using "+=" operator
list2 = ['e', 'f', 'g']
list2 += ['h']    # Now list2 is ['e', 'f', 'g', 'h'].

Lists in Python are mutable. This means that once a list has been created, we can add or remove elements from it. The append() method adds an element to the end of the list and extend() method extends the list by adding multiple elements. You can also use “+=” operator for appending multiple values:

list1 = ['a', 'b', 'c']
list1.append('d')
print(list1)   # Outputs: ['a', 'b', 'c', 'd']

list2 = ['e', 'f', 'g']
list2 += ['h']
print(list2)  # Outputs: ['e', 'f', 'g', 'h']
Hey! Do you love Python? Want to learn more about it?
Let's connect on Twitter or LinkedIn. I talk about this stuff all the time!