Python String Split

python-string-split-ipcisco

In this lesson, we will focus on Python String Split. How can we use Python String Split in Python coding? We use this string method we can divide the string according to a given seperator and returns with a list of words or strings that are build from the original string. Split function uses a parameter named str. This str parameter is used to mention the seperator parameter.

 

For example, let’s check the below code for String Split. In this code, there is a variable named cities and it has a string value consist of different cities. All these cities are seperated with a comma (,).

 

cities = "Istanbul,Paris,Amsterdam,Barcelona, New York"
print(cities.split(','))

 

The output of this python code will be like below:

 

['Istanbul', 'Paris', 'Amsterdam', 'Barcelona', 'New York']

 

python-string-split-ipcisco

 


You can check also Python String Contains


 

As you can see above, we use split() function with comma (,) operator. Here, this operatior is our seperator and according to it, we divide our main string into a list consist of smaller strings. In other words, we divide the cities string into separate city strings.

 

We can use different operators as string seperator. For example, below, we use dash (-) as seperator.

 

countries = "United Kingdom-Italy-India-Saudi Arabia-Brazil-France"
print(countries.split('-'))

 

The output of this Python String Split code will be again a list like below:

 

['United Kingdom', 'Italy', 'India', 'Saudi Arabia', 'Brazil', 'France']

 

string-split-in-python-ipcisco

 

By the way, if we do not use str parameter of split function, this function uses space as seperator by default. For example if your string is a sentence, split function divides each words as a new string in the list.

 

message = 'life is good'
print(message.split())

 

The output of this Python String Split code will give the list of the words in this sentence.

 

['life', 'is', 'good']

 

In this lesson, we have learned Python String Split. We have learned how to use split method in python programming. In other words,  we have seen how to divide a string and create a string list from the old string with this method.

 

Back to: Python Programming Course > Python Strings

Leave a Reply

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

IPCisco is the Winner of 2019 & Finalist of 2020 in “Best Certification Study Journey ” Category!

Cisco-ITBlogAwards-2019-Winner-IPCisco-k
ITBlogAwards_2020_Badge-Finalist-BestCertStudy-ipcisco

Python Programming Course

Collapse
Expand
IPCisco on Social Media!

Latest Blog Posts

metaverse-jobs-ipcisco.com
Wifi-6-vs-wifi-5-ipcisco.com
16-Reasons-To-Learn-Linux
gns3-vm-ware-installation-guide
reasons-to-have-cisco-ccna-ipcisco-1