Difference Between ‘+’ and ‘append’ in Python with examples


In Python, the + operator is used to concatenate two lists or strings together and return a new string whereas the append operator is used to add elements to the end of an existing string. The + acts as an operator whereas append() is a method in Python. In this article, we will understand the differences between the + operator and the append() method in Python.

+ operator

append() method

Purpose

concatenation

adds an element to the end

Type

operator

method

Input

Two or more strings/list

One element

output

New concatenated string/list

None

Changes original object

No

Yes

The + operator

The + operator concatenates two strings or lists together and returns a new object. The original object remains unchanged. The + operator adds a new element or string to the end of the list or string.

Example

In the below example, we concatenate two strings and two lists together using the + operator in Python.

#Concatenating two lists using ‘+’ operator
list1 = [1,2,3]
list2 = [4,5,6]
new_list = list1 + list2
print(new_list)

#Concatenating two strings using ‘+’ operator
string1 = "Hello"
string2 = "World"
new_string = string1 + " " + string2
print(new_string)

Output

[1, 2, 3, 4, 5, 6]
Hello World

The append() method

The append method is used to add a new element at the end of the original list. The original list is changed and no new object is created. So there is no return value in the case of the append() function.

Example

In the below example create a list called my_list with some initial elements in it. Then we add single, multiple, and other lists to it using the append method.

#Adding a single element to a list using append() method
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)

#Adding multiple elements to a list using append() method
my_list = [1, 2, 3]
my_list.append(4)
my_list.append(5)
my_list.append(6)
print(my_list)

#Adding a list to an existing list using append() method
my_list = [1, 2, 3]
new_list = [4, 5, 6]
my_list.append(new_list)
print(my_list)

Output

[1, 2, 3, 4]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, [4, 5, 6]]

Conclusion

In this article, we discussed the difference between the + operator and append() method in Python with suitable examples to understand the working of both. The + operator concatenates two strings or lists and returns a new object. On the other hand, the append() method adds an element to the end of the original list and does not return anything. These operators and methods can be used in various use cases in Python while working with string and lists.

Updated on: 17-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements