What is Append() function in python

22
0

In Python, the append() method is a built-in function that is used to add an item to the end of a list. It modifies the list in place and does not return a new list.

Here is an example of how to use the append() method:

my_list = [1, 2, 3]
my_list.append(4)
print(my_list)

This will output the following:

[1, 2, 3, 4]

The append() method is a quick and easy way to add an item to the end of a list. It is important to note that it only works with lists, and does not work with other data types such as strings or tuples.

If you need to add an item to the end of a string or tuple, you can use concatenation to create a new object that includes the added item. For example:

my_string = "abc"
new_string = my_string + "d"
print(new_string)

This will output the following:

"abcd"

Keep in mind that this creates a new string that includes the added item, but the original string remains unchanged.

Leave a Reply

Your email address will not be published. Required fields are marked *