Split function: 

  • The split() function is used to split the string by separating them with the split().
  • The split()  method contains the different separators  like comma(,) colon(:) semicolon(;)  and space also.
  • By default when we write split() function it will separate the values by "space ".
  • After splitting the string it returns a list of strings.
Syntax: str.split(separator,max split)
  • Example:
  • y=[int(x) for x in input("enter the 2 numbers:").slipt(",")]
  • Here, the 2 numbers are 10 2 by using split(",") these numbers are separated by"," as 10,5.
  • There are  3 parameters in the split() method
  1. separator: It separates the string by using separators if we do not use then by default it takes space.
  2. max split:  It is used to split the string by the max number of times. By default, it takes 1 i.e no limit for splitting. 
  3. Returns: After splitting the string it returns the list of string values.

# write a python code using the split().

  1. a="apple is a fruit"
  2. b=" dog is a animal"
  3. c="python is easy to learn"
  4. print(a.split())
  5. print(b.split())
  6. print(c.split())
output: ['apple' , 'is' , 'a' , 'fruit' ]
            ['dog'  ,'is'  ,'a', 'animal']
            ['pthon'  , 'is'   , 'easy'  ,'to' , 'learn']

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
Newer Post
Older Post

No comments

Leave a Reply

Select Menu