search

What is the use of the "split" method in a Python string?

The **split** method in a Python string is used to split a string into a list of substrings based on a specified separator (delimiter). By default, the separator is any whitespace character (such as a space, tab, or line break), but you can also specify a different separator.

Here's an example:

sentence = "The quick brown fox jumps over the lazy dog."
words = sentence.split()
print(words)
# Output: ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']

You can also specify a custom separator:

text = "apple,banana,cherry,date"
fruits = text.split(",")
print(fruits)
# Output: ['apple', 'banana', 'cherry', 'date']

Related Questions You Might Be Interested