Google News
logo
Python Program to Using the split() function
In the following example of Python program that demonstrates how to use the `split()` function in Python to split a string into a list of substrings based on a separator :
Program :
# define a string
string = "The quick brown fox jumps over the lazy dog"

# split the string into words
words = string.split()

# print the list of words
print(words)

# split the string based on a separator
string2 = "apple,banana,orange"
fruits = string2.split(",")
print(fruits)
Output :
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
['apple', 'banana', 'orange']
In the first example, the `split()` function is called without any arguments, which splits the string into a list of words based on whitespace characters (spaces, tabs, and newlines).

In the second example, the `split()` function is called with a comma `,` as an argument, which splits the string into a list of substrings based on the comma separator.