0

Average Words Length Python

For Average Words Length challenge in Python the function split() always returns a class ‘str’ to me rather than a class ‘list’. Anyone has seen same behavior? Thanks https://code.sololearn.com/ciImr1czh929/?ref=app

3rd Mar 2021, 7:34 PM
Sergiu Panaite
Sergiu Panaite - avatar
15 ответов
+ 3
Sergiu Panaite not to worry. It happens. Happy coding.
4th Mar 2021, 9:19 PM
Sonic
Sonic - avatar
+ 2
Can you Pls post the code to understand it..?
3rd Mar 2021, 8:04 PM
Jayakrishna 🇮🇳
+ 2
Sergiu Panaite You are using strip() function ,not split() function. strip() returns a string with removing leading and trailing spaces. split() returns a list with splitting into words..
4th Mar 2021, 11:54 AM
Jayakrishna 🇮🇳
+ 2
Jayakrishna🇮🇳 ha ha yeah that would be it.
4th Mar 2021, 1:12 PM
Sonic
Sonic - avatar
+ 2
can’t belive I was that blind, I always had impression I was writing split but was actually using strip. I feel embarassed ☹️. Thanks guys
4th Mar 2021, 8:32 PM
Sergiu Panaite
Sergiu Panaite - avatar
+ 2
Sergiu Panaite not to worry.. 👍👍
4th Mar 2021, 10:09 PM
Jayakrishna 🇮🇳
+ 1
It should return a list of strings.
3rd Mar 2021, 8:16 PM
Sonic
Sonic - avatar
+ 1
Are you calling split() with an argument of " "?
3rd Mar 2021, 8:17 PM
Sonic
Sonic - avatar
+ 1
E.g. this simple program splits a sentence into a list of words: https://code.sololearn.com/cCqt749d8X6O/?ref=app
3rd Mar 2021, 8:21 PM
Sonic
Sonic - avatar
+ 1
Actually, I learnt from David Ashton and Louis that split() without arguments does a better job at separating out the strings
4th Mar 2021, 8:44 AM
Sonic
Sonic - avatar
+ 1
Thanks Sonic , your code returns a list indeed, mine still returns a string. I’ve added the code in my original description.
4th Mar 2021, 8:55 AM
Sergiu Panaite
Sergiu Panaite - avatar
+ 1
Sergiu Panaite can you share a link to your code?
4th Mar 2021, 8:55 AM
Sonic
Sonic - avatar
0
a = input() x=(len(a)-a.count(" ")-a.count(".")-a.count(",")-a.count("?"))/(a.count(" ")+1) print(int(x)+(x>x//1)) #Hope this code is working fine; did it in a hurry, might have bugs.
5th Mar 2021, 3:20 PM
Calvin Thomas
Calvin Thomas - avatar
0
Here is the code text = input() spl = text.split() print(spl) no_char = len(text) no_elment=len(spl) print (no_char) print (no_elment) print (20/3) #print ("%.2f" % (no_char/no_elment) print("%.2f" % round((no_char/no_elment), 2)) but the output is coming as 6.666666666666667 instead of 6.0
21st Apr 2021, 3:30 PM
Siva Pitchai
0
i use text = input() word = text.split() only_alpha = text.replace(" ", "") print(len(only_alpha) / len(word))
10th May 2021, 8:15 AM
Reza Hadi Saputra
Reza Hadi Saputra - avatar