+ 1

Help needed.. How to remove punctuation from words?

Average word length code https://code.sololearn.com/cyzp2TREdYGd/?ref=app https://code.sololearn.com/cyzp2TREdYGd/?ref=app

1st Jul 2020, 2:05 PM
Omar Jafar
Omar Jafar - avatar
6 Respostas
+ 1
from string import punctuation myinput = input() for x in punctuation: # or use something like..for x in "?#~[]*&^%$£()\"\'!": myinput = myinput.replace(x, '')
1st Jul 2020, 2:19 PM
rodwynnejones
rodwynnejones - avatar
+ 6
Omar Jafar, thanks! I used a list comprehension, to create a new list, except using punctuation. The print statement puts all calculation together in one line. This can also be done in multiple steps.
1st Jul 2020, 3:47 PM
Lothar
Lothar - avatar
+ 4
This is a sample done in the sense of 'pythonic way': from math import ceil from string import punctuation ess = input().split() ess1 = [i for i in ' '.join(ess) if i not in punctuation] print(ceil((sum([len(i) for i in ess1])) / len(ess)))
1st Jul 2020, 3:06 PM
Lothar
Lothar - avatar
+ 2
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Lothar I tried your codes too, interesting logic.
1st Jul 2020, 3:35 PM
Omar Jafar
Omar Jafar - avatar
+ 1
How about string methods eg. isalpha(),isalmum() etc.? input_string = input() c=0 k=len(n.split()) for i in input_string: if(i.isalpha()): c=c+1 then you do your division and round from there.
1st Jul 2020, 2:28 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 1
Thanx you guys it worked. Used a for lor loop to replace punctuations in input string with white space So the split() returns a list of words only (without punctuations)
1st Jul 2020, 3:06 PM
Omar Jafar
Omar Jafar - avatar