Python String Lists

Python-String-Lists-ipcisco-1

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;

  • how to create string lists in python,
  • how to print string lists,
  • how to add another string to an existing string,
  • how to find any string in a string list
  • how to sort string lists
  • how to use join () function with strings

 

So, let’s start to learn Python String Lists.

 


Would you like to learn how to find Python String Length?


 

How to create string lists in python?

 

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.

 

animals = ["lion","fish","eagle","horse"]

 

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.

 


 

How to print string lists?

 

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:

 

animals = ["lion","fish","eagle","horse"]
print(animals)

 

["lion","fish","eagle","horse"]

 

You can also watch the video version of this lesson!

 


 

How to add another string to an existing string?

 

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:

 

animals1 = ["lion","fish","eagle","horse"]
animals2 = ["cat","dog"]
animals = animals1 + animals2
print(animals)

 

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:

 

['lion', 'fish', 'eagle', 'horse', 'cat', 'dog']

 

Python-String-Lists-ipcisco-2

 

 


You can check also other Python String Methods


 

How to use join () function with strings?

 

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.

 

animals = ["Lion","Fish","Eagle","Horse"]
print ("".join(animals))

 

The output will be like below:

 

LionFishEagleHorse

 

Python-String-Lists-join-ipcisco-1

 


 

How to find any string in a string list?

 

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”.

 

animals = ["Lion","Fish","Eagle","Horse"]
if "Horse" in animals:
 print("Yes")
else:
 print("No")

 

Here, the key line is

 

if "Horse" in animals:

 

The output of this code will be:

 

Yes

 

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:

 

animals = ["Lion","Fish","Eagle","Horse"]
if "Bee" in animals:
 print("Yes")
else:
 print("No")

 

No

 


 

How to sort string lists?

 

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.

 

  • Reverse – False – Ascending
  • Reverse – True – Descending

 

Let’s do an example with a fruit string list and see how python sort method works.

 

fruits = ["Banana","Strawberry","Apple",
fruits.sort()
print(fruits)

 

The output of this python code will be like below:

 

['Apple', 'Banana', 'Strawberry']

 

Python-String-Lists-sort-ipcisco-1

 

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.

 

fruits = ["Banana","Strawberry","Apple"]
fruits.sort(reverse=True)
print(fruits)

 

As you can see below, sort method has worked as descending because Reverse parameter is TRUE.

 

['Strawberry', 'Banana', 'Apple']

 

Python-String-Lists-sort-ipcisco-2

 

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.

 

Back to: Python Programming Course > Python Strings

Leave a Reply

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

Python Programming Course

Collapse
Expand