Lists of strings in Python: A list in python is an ordered sequence that can hold a variety of object types, such as, integer, character or float. A list in python is equivalent to an array in other programming languages.
- Iterating through the List
- Using join() method
- Using List Comprehension
- Using map() function
# Python program to convert a list
to string using join() function
Function to convert
def listToString(s):
# initialize an empty string
str1 = ” ”
# return string
return (str1.join(s))
# Driver code
s = [‘Newspaper’, ‘for’, ‘Blog’]
print(listToString(s))
Create list of strings
colors = ["red", "blue", "green"]
Lists of strings in Python
This post describes how to work with lists of strings in Python. Lists are one of the most common data structures in Python, and they are often used to hold strings.
Quick examples
First we’ll give some quick examples:
# define a list of strings >>> names = [“Eve”, “Alice”, “Bob”] # print the list of strings >>> print(names) [‘Eve’, ‘Alice’, ‘Bob’] # loop over the names list, and # print each string one at a time >>> for name in names: >>> print(name) Eve Alice Bob # add a name to the list >>> names.append(“Charlie”) # check if the list contains a string >>> if “Bob” in names: >>> print(“Bob is here”) Bob is here # add another string list to it >>> more_names = [“Ivan”, “Gerth”] >>> names = names + more_names # sort the list >>> names.sort() # join the strings in the list by a comma >>> comma_separated = “, “.join(names) >>> print(comma_separated) Alice, Bob, Charlie, Eve, Gerth, Ivan The next sections describe these operations on string lists in more detail.