List of Strings in Python

Lists are one of the most used data structures in Python. In this article, we will discuss how to perform different operations on a list of strings in Python.

Create a List of Strings in Python

To create a list of strings, you can use the square brackets along with the string values as follows.

Output:

Here, we have created a list of strings where each string is the name of a programming language.

If you have a container object like a set or tuple of strings, you can create the list of strings using the list() constructor. The list() constructor takes a container object as its input argument and returns a list of the elements present in the container object. From a set or tuple of strings, you can create a list of strings in python using the list() constructor as follows.

Output:

In the above example, we have created a list of strings using a set of strings and the list() constructor. The order of strings in the list is not the same in the list and set because a set is an unordered container object. Hence, the order in a set doesn’t matter. Due to this, the elements of the set aren’t added to the list in a specific order. Due to this, the order of elements in the list and the set are different.

Add String to a List of Strings in Python

To add a string to a list of strings, you can use the append() method. The append() method, when invoked on a list, accepts the string as the input argument and appends the input string at the end of the list of strings as shown below.

Output:

To insert a string at a specified position in the list of strings, you can use the insert() method. The insert() method, when invoked on a list, takes the position of the element to be inserted as the first input argument and the element to be inserted as the second input argument. After execution, the element is inserted into the list at the specified position. In the following example, we have inserted a string at index 2 of the list of strings. 

Output:

Concatenate List of Strings in Python

To concatenate the given list of strings in python, you can use the + operator as shown below.

Output:

In the output, you can observe that the concatenated list contains elements from both the original list.

Check for a String in a List of Strings in Python

To check whether a string is present in the given list of strings, we can use the membership operator “in” in python. The in operator is a binary operator. It takes the element to be searched as the first operand and the container object as the second operand. After execution, it returns True if the element is present in the container object. Otherwise, it returns False. You can check for a string in a list of strings using the in operator as shown below.

Output:

Sort List of Strings in Python

To sort a list of strings, you can directly use the sort() method. After execution, the elements of the list are sorted in alphabetical order. You can observe this in the following example. 

Output:

If you are not allowed to modify the original list, you can use the sorted() function as shown below.

Output:

In the outputs above, you can observe that the lists have been sorted alphabetically.

Sort List of Strings According to the Length of Strings

To sort the list of strings according to the length of the strings, we can pass the len() function as an input argument to the key parameter of the sort() method. You can observe this in the following example.

Output:

Alternatively, you can also use the sorted() function as shown below.

Output:

In the outputs above, you can observe that the lists have been sorted according to their length.

Conclusion

In this article, we have discussed and implemented different operations on a list of strings in python. I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *