split() method in Python String can be used to split or break a string into multiple values. These values are combined and returned as a Python List where each value is of str type.
split() method generally requires a separator. This separator is responsible to break the string into multiple values. It can be anything like a normal space, an alphabet, a number, any character or even a substring.
If separator is not specified in the split method, in all such cases, ‘ ‘(space) is considered as a String separator in a string by default.
Syntax
mainstring.split(sep)
sep: Separator that you want to use to divide or break a string (must be of string type). By default, its space.
s = "Python loves me. I love Python."
print(s.split(' '))
print(s.split('.'))
print(s.split())
O/P
[‘Python’, ‘loves’, ‘me.’, ‘I’, ‘love’, ‘Python.’]
[‘Python loves me’, ‘ I love Python’, ”]
[‘Python’, ‘loves’, ‘me.’, ‘I’, ‘love’, ‘Python.’]
One thing to note is that if the specified separator comes at the end of a string or at the beginning of a string, then in all such cases, we will get one empty string as well in the result.
That’s all here.
