string split using split() Function in python

split() Function in python splits the string into smaller chunks, or strings.  Split Function in python usually splits the string with whitespace as a separator. We can also specify the separator on which string has to be split. We will see example for both.

 

Example of Split() function in python with space separator :

Solar = "mercury venus earth mars Jupiter Saturn uranus neptune"
Solar.split(" ")

In the above example we use space as a separator to split the string, so the output will be

[‘mercury’, ‘venus’, ‘earth’, ‘mars’, ‘Jupiter’, ‘Saturn’, ‘uranus’, ‘neptune’]

 

 

Example of Split() function in python with comma separator :

Countries = "Russia,UK,USA,Singapore,France,EU,India,China"
Countries.split(",")

In the above example we use comma as a separator to split the string, so the output will be

[‘Russia’, ‘UK’, ‘USA’, ‘Singapore’, ‘France’, ‘EU’, ‘India’, ‘China’]

 

 

Example of python Split() function with hyphen separator :

Hyphenword = "state-of-the-art"
Hyphenword.split("-")

In the above example we use hyphen as a separator to split the string, so the output will be

[‘state’, ‘of’, ‘the’, ‘art’]

 

previous split() function in python                                                                                                                 next split() function in python