+ 2

What is split function used for?

11th Jul 2021, 8:03 AM
Shreya Dhurde
Shreya Dhurde - avatar
6 Answers
+ 6
Shreya , let me give you an example with this task description: â–Ștake a sentence as input and calculate the average word lenght in this case a sentence is used like: song = "this is major tom to ground control" to do this task, you need to: â–Ș separate the words to individual strings and put them to a list for further processing â–Ș to do so split() is used. in case of a space should be used to separate, no argument is needed: words = song.split() â–Ș the result will be : ['this', 'is', 'major', 'tom', 'to', 'ground', 'control'] â–Șnow further processing can be done: iterate through list and count the characters of each word and calculate a running sum for total number of characters. finally use the total number of characters and divide it by the number of words happy coding!
11th Jul 2021, 10:41 AM
Lothar
Lothar - avatar
+ 7
To be accurate, you should probably call split() a method, rather than a function. Methods are called on objects, in this case the string, and the object and the method are connected by a dot. There are many built-in string methods but only a few built-in function that work with strings, e.g. len(). Here are all the string methods https://code.sololearn.com/clzf6rNcraj8
28th Jul 2021, 11:24 PM
David Ashton
David Ashton - avatar
+ 4
it returns a list, using split's argument as the point to "split" the string, wherever the character or characters are in the string. a = "mississippi" b = a.split('i') c = a.split("si") print(b) print(c) # output ["m", "ss", "ss", "pp"] ["mis", "s", "ppi"]
11th Jul 2021, 8:06 AM
Slick
Slick - avatar
+ 4
I can't explain in such good manner but according to me as I have used it so much in that, It is used mostly in handling multiple inputs in one line in my case, Usually we get many integer inputs with spaces in coding websites, so to replace cin<<X<<" "<<... N times //C++ We use list comprehension [int(i) for i in input.split()]
11th Jul 2021, 1:33 PM
Shubham Aggarwal
Shubham Aggarwal - avatar
+ 4
Thank You all
12th Jul 2021, 6:20 AM
Shreya Dhurde
Shreya Dhurde - avatar