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. List can contain any type of data type. Let’s discuss certain ways in which we can perform string append operation in 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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 string element to end of list without changing the state of string to list of characters.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.