Python String | split()

split() method returns a list of strings after breaking the given string by the specified separator.

Syntax :

str.split(separator, maxsplit)

Parameters :

separator : The 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 there is no limit.

Returns :

returns a list of strings after breaking the given string by the specified separator.

CODE 1

filter_none

edit
close

play_arrow

link
brightness_4
code

text = 'geeks for geeks'
  
# Splits at space
print(text.split())
  
word = 'geeks, for, geeks'
  
# Splits at ','
print(word.split(', '))
  
word = 'geeks:for:geeks'
  
# Splitting at ':'
print(word.split(':'))
  
word = 'CatBatSatFatOr'
  
# Splitting at 3
print([word[i:i+3] for i in range(0, len(word), 3)])

chevron_right


Output :

['geeks', 'for', 'geeks']
['geeks', 'for', 'geeks']
['geeks', 'for', 'geeks']
['Cat', 'Bat', 'Sat', 'Fat', 'Or']

CODE 2

filter_none

edit
close

play_arrow

link
brightness_4
code

word = 'geeks, for, geeks, pawan'
  
# maxsplit: 0
print(word.split(', ', 0))
  
# maxsplit: 4
print(word.split(', ', 4))
  
# maxsplit: 1
print(word.split(', ', 1))

chevron_right


Output :

['geeks, for, geeks, pawan']
['geeks', 'for', 'geeks', 'pawan']
['geeks', 'for, geeks, pawan']


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

8


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.