Python | Append String to list
Sometimes, while working with data, we can have a problem in which we need to add elements to a container. The list can contain any type of data type. Let’s discuss certain ways in which we can perform string append operations in the list of integers.
Method #1 : Using + operator + list conversion In this method, we first convert the string into a list and then perform the task of append using + operator.
Python3
# Python3 code to demonstrate working of# Appending String to list# using + operator + list conversion# initialize listtest_list = [1, 3, 4, 5]# initialize stringtest_str = 'gfg'# printing original listprint("The original list : " + str(test_list))# printing original stringprint("The original string : " + str(test_str))# Appending String to list# using + operator + list conversiontest_list += [test_str]# printing resultprint("The list after appending is : " + str(test_list)) |
The original list : [1, 3, 4, 5] The original string : gfg The list after appending is : [1, 3, 4, 5, 'gfg']
Method #2: Using append() This particular function can be used to perform the operation of appending a string element to the end of a list without changing the state of the string to a list of characters.
Python3
# Python3 code to demonstrate working of# Appending String to list# using append()# initialize listtest_list = [1, 3, 4, 5]# initialize stringtest_str = 'gfg'# printing original listprint("The original list : " + str(test_list))# printing original stringprint("The original string : " + str(test_str))# Appending String to list# using append()test_list.append(test_str)# printing resultprint("The list after appending is : " + str(test_list)) |
The original list : [1, 3, 4, 5] The original string : gfg The list after appending is : [1, 3, 4, 5, 'gfg']
Method#3: Using insert() This function is used to insert and add the element at the last of the list by using the length of the list as the index number.
Python3
# Python3 code to demonstrate working of# Appending String to list# using insert()# initialize listtest_list = [1, 3, 4, 5]# initialize stringtest_str = 'gfg'# printing original listprint("The original list : " + str(test_list))# printing original stringprint("The original string : " + str(test_str))# Index for insert methodindex = len(test_list)# Appending String to list# using insert()test_list.insert(index, test_str)# printing resultprint("The list after appending is : " + str(test_list)) |
The original list : [1, 3, 4, 5] The original string : gfg The list after appending is : [1, 3, 4, 5, 'gfg']
Method#4: Using extend(): This method can be used to solve this problem, extend function is used to merge the one list to the end of second list. We add a string to the end of the list by using extend function.
Python3
# Python3 code to demonstrate working of# Appending String to list# using extend()# initialize listtest_list = [1, 3, 4, 5]# initialize stringtest_str = 'gfg'# printing original listprint("The original list : " + str(test_list))# printing original stringprint("The original string : " + str(test_str))# Appending String to list# using extend()test_list.extend([test_str])# printing resultprint("The list after appending is : " + str(test_list)) |
The original list : [1, 3, 4, 5] The original string : gfg The list after appending is : [1, 3, 4, 5, 'gfg']


