Python List of Strings


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"]


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']





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


elves = ["Arwen", "Legolas", "Elrond", "Galadriel"]







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"]


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


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




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"]


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


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




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]


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



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





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


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








Back to: Python Programming Course > Python Lists

Leave a Reply

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

IPCisco is the Winner of 2019 “Best Certification Study Journey” Category! We are also Finalist of 2020 & 2021 in Cisco IT Blog Awards!


Python Programming Course

IPCisco on Social Media!