In Python Coding Lesson, we will focus on Python String Lists. Lists are a common data structure used in Python. Beside, string are also one of the most used data structures. Here, we will see list of strings in Python.
In this Python String Lists lesson, we will learn;
So, let’s start to learn Python String Lists.
Would you like to learn how to find Python String Length?
Table of Contents
To create a string list in python, we will do the same things that we do for creating normal lists. Here, the only difference is the members of the lists. Because, in Python string lists, the list members are strings.
As you can see below, we an create a string list with different string members and we can assign it to a variable. Bellow, we will create a string list that contains different animals and we will assign it to animals variable.
If we use only this command on python command line, our string is created and assigned but we do not see anything on the screen. To see this list, we should use print function with animals variable.
We have created our python string list and we would like to see the content now. To do this, we will sue print function in python.
If we use the below code, we will see our string as an output:
You can also watch the video version of this lesson!
Different lists needed to be combine for different reasons. When we combine one more strings, a new string is created with the memebrs of these two strings. To combine one more string in python, we use plus (+) sign.
Let’s do an example like below:
As you can see above, we have two different strings and we combine these two strings with plus (+) sign.
The output of the above python string list code will be like below:
You can check also other Python String Methods
With python join function, we can combine all the strings in a string list as one string. To see this behaviour better, let’s see an example.
The output will be like below:
Finding any value in a string list is a widely used methods in Python String Lists. To do this, we will use in keyword.
As you can see below, we have a string list containing different animals. Here, we will look for “Horse” in this string list and if we find it, then we will print “Yes”, if not we will print “No”.
Here, the key line is
The output of this code will be:
Because there is a “Horse” string in this python string list.
The below code will return, No,because the requested animal is not in the list:
For variety of reasons, we need to sort strings in a list. To do this, we willl use sort () method in python. By default, sort () method sorts the strings in the string list ascending, from A to Z. We can change it with a parameter of sort method as descending, from Z to A.
The parameter that is used with sort method is called reverse. By default reverese is False and sort method is ascending. To set it as descending, we set reverse parameter as True.
Let’s do an example with a fruit string list and see how python sort method works.
The output of this python code will be like below:
As you can see, our string list is sorted as ascending, from A to Z. Because by default sort method is ascending, reverese parameter is False. Let’s do another example and at this time, let’s set reverse parameter as TRUE.
As you can see below, sort method has worked as descending because Reverse parameter is TRUE.
In this lesson,m we have learned Python String Lists. How to create Python String Lists, how to modify them and how to use different methods with this one of the most used list types of python.
Leave a Reply