Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

spilt function in python

 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']





This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

spilt function in python

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×