Python

Python Add String to List

Anything inside quotation marks (single quote or double quote) forms a string. Strings in python signify Unicode chars. However, a single char in Python refers to a string with a size of 1. We use square braces to fetch string values. Strings in python are “absolute,” which means you can’t modify strings once they are created. Since they can’t be altered, we create new filtered strings to signify calculated items. In this guide, you’ll learn how to add a string to a python list with the help of different example codes. We use the Spyder compiler to illustrate every single program code. Come let’s move to its implementation.

Example 1:

We use the append() function to append a string item to the endpoint of the list without altering the string state to the characters list. The append() method inserts a particular value to the current list. The method doesn’t return a new filtered list of elements but will alter the current list by placing the number to the endpoint of the list.

To further illustrate the function, open Spyder IDE and create a new file where you write code to append a string to a list using the append method. Initially, we create and initialize a list of integers. Then we create a string and use two print statements. These statements display the list of integers and a newly created string. Once done, we can then call the append method and pass the string as a parameter. In the end, the last print function displays the appending list.

  • my_list = [2, 4, 6, 8 ]
  • my_str = ‘sam’
  • print(“My newly created list is: ” + str(mu=y_list))
  • print(“My newly created string is: ” + str(my_str))
  • test_list.append(my_str)
  • print(“After applying appending method : ” + str(my_list))

To view the output of the append() method, just save and execute the program file, and the filtered list is displayed on the console screen.

Example 2:

Next, we use the insert() method to add string elements to the python list. The differentiation point between insert() and append() is that the insert method augments a specific item at a stated list of the index. On the other hand, append() can add the item only at the endpoint of the python list.

To further illustrate the insert() method, head over to the program file and start writing your program code to insert string to list. Initially, we create and initialize a list of integers. Then we use print statements with the “f” prefix. F-strings provide a unique and efficient way to insert python expressions in the string typos for formatting. Here we use another statement that gets a number from a user to place it into the list.

To move further, we use another statement that takes the list index from the user as an insert method to add a number in the specified index. We can then call the insert function and pass two parameters in it i.e., list_index and get_num. In the end, the last print function displays the result on a specified index.

  • my_list = [2, 4, 6, 8]
  • print(f’My original List is {my_list}’)
  • get_num = int(input(“User enter anylist number to add to list:\n”))
  • list_index = int(input(f’User enter index b/w 0 and {len(my_list) – 1}:\n’))
  • my_list.insert(list_index, get_num)
  • print(Filtered Numbers List {my_list}’)

Come let’s save and execute the program file and check how the insert() method inserts an element in the specified index number.

Example 3:

In our last example code, we use extend() method to add string elements to the python list. The extend() is an inbuilt Python function that places the given list items to the endpoint of the original list. The differentiation point between append() and extend() is that append adds its parameter as a single object to the list end while extending () traverses over its argument and places each item to the python list, and increases the list length.

The size of the list extends by placing several elements in it. So here in our code, we first create a blank list. Then we call the extend() function, which extends the size of the list by adding 0 and 1 elements and then printing the given extended list. We call extend() function again and again, which adds the elements in the list and extends the size of the list.

  • My_list = []
  • My_list.extend([0, 1])
  • print(my_list)
  • my_list.extend((3, 4))
  • print(my_list)
  • my_list.extend(“Python”)
  • print(my_list)

Again, save and execute the program file and check how the () method extends the list size by placing elements.

Conclusion:

With the help of the aforementioned methods, you can now easily add elements to a python List. You can not only add elements to the list but also extend the list size. All three examples will help you out to complete your work.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content