Python split() method is used to break a string into a list of smaller strings based on a specified delimiter. It is commonly used for text parsing, string extraction and processing CSV or space-separated data.
s = "one,two,three"
words = s.split(',')
print(words)
Output
['one', 'two', 'three']
Explanation:
- s.split(','): splits the string s at every comma.
- It returns a list of the parts: ['one', 'two', 'three'].
Syntax
str.split(separator, maxsplit)
Parameters
- separator (optional): The delimiter at which the string is split. Default is whitespace.
- maxsplit (optional): Maximum number of splits. Default is -1, which means no limit.
Returns
- Returns a list of strings after breaking the given string by the specified separator.
Splitting by Whitespace and Other Delimiters
text = 'geeks for geeks'
print(text.split())
word = 'geeks, for, geeks'
print(word.split(','))
word = 'geeks:for:geeks'
print(word.split(':'))
word = 'CatBatSatFatOr'
print(word.split('t'))
Output
['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']
Note: When no separator is given, multiple consecutive spaces are treated as a single separator.
text = "Hello world"
print(text.split())
Output
['Hello', 'world']
Using maxsplit
The maxsplit parameter is used to control how many splits to return after the string is parsed. Even if there are multiple splits possible, it'll only do maximum that number of splits as defined by the maxsplit parameter.
word = 'geeks, for, geeks, hello'
print(word.split(', ', 0))
print(word.split(', ', 4))
print(word.split(', ', 1))
Output
['geeks, for, geeks, hello'] ['geeks', 'for', 'geeks', 'hello'] ['geeks', 'for, geeks, hello']
Explanation:
- maxsplit=4 splits up to 4 times.
- maxsplit=1 splits only once.
Parsing a Sentence
String parsing involves splitting a string into smaller segments based on a specific delimiter or pattern. This can be easily done by using a split() method in Python.
text = "Hello geek, Welcome to GeeksforGeeks."
result = text.split()
print(result)
Output
['Hello', 'geek,', 'Welcome', 'to', 'GeeksforGeeks.']