###:- split() method in Python split a string into a list of strings after breaking the given string by the specified separator. Syntax : str.split(separator, maxsplit) Parameters : separator : This is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator. maxsplit : It is a number, which tells us to split the string into maximum of provided number of times. If it is not provided then the default is -1 that means there is no limit. Returns : Returns a list of strings after breaking the given string by the specified separator.
## First Example:--
text = 'geeks for geeks'
1- print(text.split())
word = 'geeks, for, geeks'
2- print(word.split(','))
word = 'geeks:for:geeks'
3- print(word.split(':'))
word = 'CatBatSatFatOr'
4- print(word.split('t'))
## Output:-- 1- ['geeks', 'for', 'geeks']
2- ['geeks', ' for', ' geeks']
3- ['geeks', 'for', 'geeks']
4- ['Ca', 'Ba', 'Sa', 'Fa', 'Or'] ## Second Example:-- word = 'geeks, for, geeks, pawan'
1- print(word.split(', ', 0))
2- print(word.split(', ', 4))
3- print(word.split(', ', 1))
## Output:--
1- ['geeks, for, geeks, pawan']
2- ['geeks', 'for', 'geeks', 'pawan']
3- ['geeks', 'for, geeks, pawan']
|
['geeks', 'for', 'geeks']
['Ca', 'Ba', 'Sa', 'Fa', 'Or
['geeks', ' for', ' geeks']
['geeks', 'for', 'geeks']
['Ca', 'Ba', 'Sa', 'Fa', 'Or']
['geeks', ' for', ' geeks']
['geeks', 'for', 'geeks']
['Ca', 'Ba', 'Sa', 'Fa', 'Or']
Comments
Post a Comment