List of strings in Python
Last Updated :
20 Nov, 2024
A list of strings in Python stores multiple strings together. In this article, we’ll explore how to create, modify and work with lists of strings using simple examples.
Creating a List of Strings
We can use square brackets [] and separate each string with a comma to create a list of strings.
Python
a = ["apple", "banana", "cherry"]
print(a)
Output['apple', 'banana', 'cherry']
Explanation: 'a' is a list containing three string elements: "apple", "banana", and "cherry".
Note: We can also use list() constructor to create a list of strings.
Accessing Elements in a List
We can access elements in a list using their index. Python supports both positive indexing (from the start) and negative indexing (from the end).
Python
a = ["apple", "banana", "cherry"]
# First element using positive index
print(a[0])
# Last element using negative index
print(a[-1])
# Second last element
print(a[-2])
Outputapple
cherry
banana
Explanation:
- Positive indexing: Starts from 0. So, a[0] retrieves "apple".
- Negative indexing: Starts from -1 for the last element. So, a[-1] retrieves "cherry" and a[-2] retrieves "banana".
Adding Elements to a List
We can add elements to a list using the append() or insert() methods.
Python
a = ["apple", "banana"]
# Add at the end of list
a.append("cherry")
# Add at index 1
a.insert(1, "orange")
print(a)
Output['apple', 'orange', 'banana', 'cherry']
Explanation:
- append("cherry") adds "cherry" to the end of the list.
- insert(1, "orange") places "orange" at the second position (index 1).
Removing Elements from a List
To remove elements from list, we can use remove(), pop(), or del().
Python
a = ["apple", "banana", "cherry"]
# Remove by value
a.remove("banana")
# Remove by index
val = a.pop(1)
print(val)
# Delete first element
del a[0]
print(a)
Explanation:
- remove("banana") deletes "banana" from the list.
- pop(1) removes the second element and stores it in val.
- del a[0] deletes the first element.
Modifying Strings in a List
To modify the strings in a list we can use index to modify elements directly.
Python
a = ["apple", "banana", "cherry"]
a[1] = "orange"
print(a)
Output['apple', 'orange', 'cherry']
Explanation: a[1] = "orange" replaces "banana" with "orange".
Iterating through a List
We can use a for loop to iterate through all strings in the list.
Python
a = ["apple", "banana", "cherry"]
for s in a:
print(s)
Outputapple
banana
cherry
Similar Reads
Create a List of Strings in Python Creating a list of strings in Python is easy and helps in managing collections of text. For example, if we have names of people in a group, we can store them in a list. We can create a list of strings by using Square Brackets [] . We just need to type the strings inside the brackets and separate the
3 min read
Find Length of String in Python In this article, we will learn how to find length of a string. Using the built-in function len() is the most efficient method. It returns the number of items in a container. Pythona = "geeks" print(len(a)) Output5 Using for loop and 'in' operatorA string can be iterated over, directly in a for loop.
2 min read
Python | List of tuples to String Many times we can have a problem in which we need to perform interconversion between strings and in those cases, we can have a problem in which we need to convert a tuple list to raw, comma separated string. Let's discuss certain ways in which this task can be performed. Method #1: Using str() + str
8 min read
How to sort a list of strings in Python In this article, we will explore various methods to sort a list of strings in Python. The simplest approach is by using sort().Using sort() MethodThe sort() method sorts a list in place and modifying the original list directly.Pythona = ["banana", "apple", "cherry"] # Sorting list in place a.sort()
2 min read
Python | Append String to list Sometimes, while working with data, we can have a problem in which we need to add elements to a container. The list can contain any type of data type. Let's discuss certain ways in Python in which we can perform string append operations in the list of integers.Example: Append String at the end of a
5 min read