Split function in python

 

###:- 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'
  
# Splits at space
1- print(text.split())
  
word = 'geeks, for, geeks'
  
# Splits at ','
2- print(word.split(','))
  
word = 'geeks:for:geeks'
  
# Splitting at ':'
3- print(word.split(':'))
  
word = 'CatBatSatFatOr'
  
# Splitting at t
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'
  
# maxsplit: 0
1- print(word.split(', ', 0))
  
# maxsplit: 4
2- print(word.split(', ', 4))
  
# maxsplit: 1
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

Popular posts from this blog

XAMPP, SQL BASIC COMMANDS

The Minion Game Hackerrank Solution

Arrays - DS | HackerRank Solutions