Python String | split()
View Discussion
Improve Article
Save Article
Like Article
- Difficulty Level : Basic
- Last Updated : 08 Jul, 2022
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.
Example 1: Example to demonstrate how split() function works
text='geeks for geeks'# Splits at space(text.split())word='geeks, for, geeks'# Splits at ','(word.split(','))word='geeks:for:geeks'# Splitting at ':'(word.split(':'))word='CatBatSatFatOr'# Splitting at t(word.split('t'))Output :
['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']Example 2: Example to demonstrate how split() function works when maxsplit is specified
word='geeks, for, geeks, pawan'# maxsplit: 0(word.split(', ',0))# maxsplit: 4(word.split(', ',4))# maxsplit: 1(word.split(', ',1))Output :
['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']My Personal Notes arrow_drop_upRecommended ArticlesPage :Article Contributed By :



