How to count words in string with punctuation in Python

1 Answer

0 votes
import string

s = 'python! ,,c, c++. c# $$$java@# php.'

result = sum([i.strip(string.punctuation).isalpha() for i in s.split()])

print(result)



'''
run:

6

'''

 



answered Feb 19, 2022 by avibootz
edited Nov 2 by avibootz
...