Skip to content
Related Articles

Related Articles

Python | Append String to list

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 16 Jun, 2022

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 list
test_list = [1, 3, 4, 5]
 
# initialize string
test_str = 'gfg'
 
# printing original list
print("The original list : " + str(test_list))
 
# printing original string
print("The original string : " + str(test_str))
 
# Appending String to list
# using + operator + list conversion
test_list += [test_str]
 
# printing result
print("The list after appending is : " + str(test_list))

Output

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 list
test_list = [1, 3, 4, 5]
 
# initialize string
test_str = 'gfg'
 
# printing original list
print("The original list : " + str(test_list))
 
# printing original string
print("The original string : " + str(test_str))
 
# Appending String to list
# using append()
test_list.append(test_str)
 
# printing result
print("The list after appending is : " + str(test_list))

Output

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 list
test_list = [1, 3, 4, 5]
 
# initialize string
test_str = 'gfg'
 
# printing original list
print("The original list : " + str(test_list))
 
# printing original string
print("The original string : " + str(test_str))
 
# Index for insert method
index = len(test_list)
# Appending String to list
# using insert()
test_list.insert(index, test_str)
 
# printing result
print("The list after appending is : " + str(test_list))

Output

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 list
test_list = [1, 3, 4, 5]
 
# initialize string
test_str = 'gfg'
 
# printing original list
print("The original list : " + str(test_list))
 
# printing original string
print("The original string : " + str(test_str))
 
 
# Appending String to list
# using extend()
test_list.extend([test_str])
 
# printing result
print("The list after appending is : " + str(test_list))

Output

The original list : [1, 3, 4, 5]
The original string : gfg
The list after appending is : [1, 3, 4, 5, 'gfg']

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!