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?


import_contacts

You may also like:

Related Posts

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusionWas this post helpful? Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

  • 11 June

    Check if Object Is Iterable in Python

    Table of ContentsWhat are iterables in Python?How to check if object is iterable in Python?Using the iter() function to check if object is iterable in PythonUsing the for loop to check if object is iterable in PythonUsing the isinstance() function to check if object is iterable in PythonConclusionWas this post helpful? In this post, we […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.