Python List of Strings

python-list-of-strings-1

In lesson, we will focus on Python list of strings. In python lists we can list and python data structures. Here, we will see one of them, Python list of strings. Then, let’s give example of these string lists to understand better these types of lists in python.

 

First of all, let’s show basic string list in python with an example.

 


You can also learn how to Sort a Python list


 

elves = ["Arwen", "Legolas", "Elrond", "Galadriel"]
print(elves)

 

In this string list, we have 4 members and all of them are string. The output of this python string will be like below:

 

['Arwen', 'Legolas', 'Elrond', 'Galadriel']

 

 

python-list-of-strings-1

 

If we use python len() function for this python string list, we will see 4 as a return.

 

elves = ["Arwen", "Legolas", "Elrond", "Galadriel"]
print(len(elves))

 

4

 

 

python-list-of-strings-2

 

Now, let’s use an important python function that we use with Python list of strings. This is python sort() function. Here, we will sort out python string list as ascending (A to Z). So, we will not use any parameter with sort () function.

 

elves = ["Arwen", "Legolas", "Elrond", "Galadriel"]
elves.sort()
print(elves)

 

As you can see below, our python string list is sorted as ascending (A to Z).

 

['Arwen', 'Elrond', 'Galadriel', 'Legolas']

 

python-list-of-strings-3

 

Now, let’s do this sorting as descending (Z to A). For this purpose, we will  use reverse parameter and we will set it as True. In the previous exaample we did not use this parameter and by default iit is accepted as False.

 

elves = ["Arwen", "Legolas", "Elrond", "Galadriel"]
elves.sort(reverse=True)
print(elves)

 

Here, our string is sorted as descending (Z to A).

 

['Legolas', 'Galadriel', 'Elrond', 'Arwen']

 

python-string-lists-1-ipcisco

 

We can also modify the members of Python list of strings. We can change all the members as uppercase, lowercase or we can capitalize each member in the list. Let’s give examples for these functiions that are used with Python list of strings.

 

 

Below firstly, we will convert all the members of python string list to lower case  firtly. Then we will convert all memebrs to upper case. Lastly, we will convert another list that has members consist of all lower cases and we will capitalize it. In other words all the members of the list will start with an upper case again.

 

elves = ["Arwen", "Legolas", "Elrond", "Galadriel"]
elves = [x.lower() for x in elves]
print(elves)

 

['arwen', 'legolas', 'elrond', 'galadriel']

 

 

elves = ["Arwen", "Legolas", "Elrond", "Galadriel"]
elves = [x.upper() for x in elves]
print(elves)

 

['ARWEN', 'LEGOLAS', 'ELROND', 'GALADRIEL']

 

 

elves = ['arwen', 'legolas', 'elrond', 'galadriel']
elves = [x.capitalize() for x in elves]
print(elves)

 

['Arwen', 'Legolas', 'Elrond', 'Galadriel']

 

 

python-string-lists-3-ipcisco

 

 

 

 

Back to: Python Programming Course > Python Lists

Leave a Reply

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

Python Programming Course

Collapse
Expand