List of Strings in Python: Implementations with Examples

The list is a data structure that allows you to create multiple objects in a single variable. It is mutable as you can append, pop or remove elements from it.  In this post, you will grasp a depth understanding of the list of strings in python with various examples.

Examples on the list of strings in python

Example 1: Create a list of strings in python

You can create a list of strings by putting more than one string in a square bracket.

list_strings = ["ABC","DEF","GHI","JKL"]

Output

["ABC","DEF","GHI","JKL"]

Example 2: Iterate and print a list of strings

You have to use a loop for iterating over the list of strings. The simple way to iterate over the list is for loop.

list_strings = ["ABC","DEF","GHI","JKL"]
for i in list_strings:
  print(i)

Output

ABC
DEF
GHI
JKL

Example 3: Append a string to the existing list of strings

You will use the append() function to append a string to the existing list. Pass the new string you want to append as an argument.

list_strings = ["ABC","DEF","GHI","JKL"]
list_strings.append("MNP")
print(list_strings)

Output

['ABC', 'DEF', 'GHI', 'JKL', 'MNP']

Example 4: Remove a specific word from the list of strings

You can use the remove() function to remove the specific word from the list of strings. For example, if I want to remove the string “DEF” then use the below lines of code.

list_strings = ["ABC","DEF","GHI","JKL"]
list_strings.remove("DEF")
print(list_strings)

Output

['ABC', 'GHI', 'JKL']

Example 5: How to find a string in a list of strings

To find strings in a list of strings in a strings list you will use the if else conditions. For example, let’s check whether the “ABC” string is in the list or note.

list_strings = ["ABC","DEF","GHI","JKL"]
if "ABC" in list_strings:
  print("Yes ABC is in list of strings")
else:
  print("No, ABC is not in the list of strings")

Output

Yes ABC is in list of strings

Example 6: Concatenate a list of strings python

You can easily concatenate the list of strings with another list using the “+” operator.

list_strings_1 = ["ABC","DEF","GHI","JKL"]
list_strings_2 = ["MNP","QRS"]
list_strings_1 + list_strings_2

Output

['ABC', 'DEF', 'GHI', 'JKL', 'MNP', 'QRS']

Example 7: Write the list of strings to a file

Firstly you have to open the file stream as the write mode using the while loop and inside it iterate each item in the list and write to the file using the write() function.

list_strings_ = ["ABC","DEF","GHI","JKL"]
#open file
with open('abc.txt', 'w+') as f:   
    # write elements of list
    for elements in list_strings_:
        f.write('%s\n' %elements)
     
    print("File written successfully")
 
# close the file
f.close()

Output

File written successfully
list of strings to a file
list of strings to a file

Example 7: Sort a list of strings alphabetically in python

You can sort the list of strings alphabetically using the sort() function. Use your_list.sort() to sort the elements.

list_strings = ["ABC","GHI","JKL","DEF"]
list_strings.sort()
print(list_strings)

Output

['ABC', 'DEF', 'GHI', 'JKL']

Example 8: Compare a list of strings

To achieve this task you will use the zip of the two lists and compare each element of list1 and list2. If it is equal then append it to the final list.

list1 = ["ABC","DEF","GHI","JKL"]
list2 = ["GHI","JKL"]
final_list=[]
for i,j in zip(list1,list2):
  if i==j:
    final_list.append(i)
print(final_list)

Output

["GHI","JKL"]

Conclusion

A list of strings is mostly used in the data structure. In this post, you have learned how to create, iterate, append e.t.c in the list of strings. The above examples are the most implemented part for the coders.

Keep coming back to our site as more examples will be added in the near future.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link