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.
- 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
- separator: It separates the string by using separators if we do not use then by default it takes space.
- 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.
- Returns: After splitting the string it returns the list of string values.
# write a python code using the split().
- a="apple is a fruit"
- b=" dog is a animal"
- c="python is easy to learn"
- print(a.split())
- print(b.split())
- print(c.split())
output: ['apple' , 'is' , 'a' , 'fruit' ]
['dog' ,'is' ,'a', 'animal']
['pthon' , 'is' , 'easy' ,'to' , 'learn']
No comments