To create a list of strings in Python, add comma-separated strings in between square brackets.
For example, here is a list of strings that represent names:
names = ["Alice", "Bob", "Charlie"]
Now that you have a list of strings, there are lots of things you might want to do with it.
In the following, you are going to learn 10+ most common examples of what you commonly want to do with a list of strings.
List of Strings in Python
A list is a commonly used type in Python. It is used to store multiple elements in one place for easy access, update, and modification.
It is common to deal with a list of strings in Python. That’s why you are going to learn some useful things you can do with it.
Let’s start by creating a list of strings. Syntactically, there are two ways to do this.
To create a list of strings in Python, add comma-separated strings inside of square brackets. Remember to add double quotation marks around each string.
For instance:
names = ["Alice", "Bob", "Charlie"]
You can also declare the list of strings using multiple lines.
For instance:
names = [ "Alice", "Bob", "Charlie" ]
Sometimes the expression is more readable this way. For example, when you create a long list of strings, it may be wise to spread the expression across multiple lines.
Next, let’s see some useful things you might want to do with a list of strings.
How to Print a List of Strings in Python
If you want to print the list of strings as-is, just pass it into the print()
function.
For example:
names = ["Alice", "Bob", "Charlie"] print(names)
Output:
['Alice', 'Bob', 'Charlie']
If you want to print the strings one by one, use a for-loop.
For example:
names = ["Alice", "Bob", "Charlie"] for name in names: print(name)
Output:
Alice Bob Charlie
How to Change the Case of List of Strings
1. Lowercase
To convert a string to lowercase in Python, use the built-in lower()
method of a string.
To convert a list of strings to lowercase, use a loop.
For example:
names = ["Alice", "Bob", "Charlie"] names_lower = [] for name in names: names_lower.append(name.lower()) print(names_lower)
Output:
['alice', 'bob', 'charlie']
Alternatively, you can also use a list comprehension. This is essentially a one-liner for-loop.
For instance:
names = ["Alice", "Bob", "Charlie"] names_lower = [name.lower() for name in names] print(names_lower)
Output:
['alice', 'bob', 'charlie']
2. Uppercase
To turn a string to uppercase in Python, use the built-in upper()
method of a string.
To turn a list of strings to uppercase, loop through the list and convert each string to upper case.
For instance:
names = ["alice", "bob", "charlie"] names_upper = [name.upper() for name in names] print(names_upper)
Output:
['ALICE', 'BOB', 'CHARLIE']
3. Capitalize the First Letters
To capitalize the first letter of a string, call the capitalize()
method.
To capitalize a whole list of strings, use a list comprehension (or for-loop) and capitalize the strings one by one.
For example:
names = ["alice", "bob", "charlie"] names_cap = [name.capitalize() for name in names] print(names_cap)
Output:
['Alice', 'Bob', 'Charlie']
How to Sort a List of Strings in Python
Sorting data is an important ability. In Python, sorting is easy with the built-in sorted()
function. Let’s see a couple of useful examples.
1. Alphabetic Sort
A very basic way to sort text data is by sorting it in alphabetical order.
In Python, calling sorted()
function on a list of strings creates an alphabetical ordering of the list.
For example:
names = ["Charlie", "Alice", "Bob"] sorted_names = sorted(names) print(sorted_names)
Output:
['Alice', 'Bob', 'Charlie']
2. Reversed Alphabetic Sort
Sometimes you want to reverse the alphabetical ordering. To do this, you can set reverse
parameter True
in the sorted()
function.
For example:
names = ["Charlie", "Alice", "Bob"] sorted_names = sorted(names, reverse=True) print(sorted_names)
Output:
['Charlie', 'Bob', 'Alice']
3. Sort by Length
To sort a list of strings by length (shortest first), specify the key
parameter inside the sorted()
function call.
For example:
names = ["Charlie", "Alice", "Bob"] sorted_names = sorted(names, key=len) print(sorted_names)
Output:
['Bob', 'Alice', 'Charlie']
If you want to sort a list of strings in a reversed length order (longest first), you can do:
names = ["Charlie", "Alice", "Bob"] sorted_names = sorted(names, key=len, reverse=True) print(sorted_names)
Output:
['Charlie', 'Alice', 'Bob']
How to Merge a List of Strings
To combine a list of strings, use the join()
method of a string.
For example, to join a list of words to form a sentence, you can call:
words = ["This", "is", "a", "test"] sentence = " ".join(words) print(sentence)
Output:
This is a test
Here the first string is " "
. It acts as the separator between the combined strings.
The separator can really be anything.
For example, if you want to combine the strings by a new line, you can do:
words = ["This", "is", "a", "test"] sentence = "\n".join(words) print(sentence)
Output:
This is a test
How to Filter a List of Strings in Python
To filter a list based on a criterion, you can use the built-in filter()
function.
This function takes:
- A lambda expression. This is an operation that acts as a filtering criterion.
- A list to be filtered.
For example, let’s filter a list of names that contain the letter 'o'
:
names = ["Charlie", "Alice", "Bob", "David"] names_with_o = list(filter(lambda name: 'o' in name, names)) print(names_with_o)
Output:
['Bob']
In case you are unfamiliar with lambda expressions, check out this guide for more details.
How to Convert a List of Strings into a List of Integers
Sometimes you may get data as a list of numbers. But the numbers are strings. In this case, you want to convert them to integers.
To convert a list of strings to a list of integers, use a for loop or a list comprehension.
For instance:
num_strings = ["1", "2", "3", "4"] numbers = [int(number) for number in num_strings] print(numbers)
Output:
[1, 2, 3, 4]
Thanks for reading. I hope you find it useful. Happy coding!