在本文中,我们将学习解决给定问题陈述的解决方案和方法。
给我们一个句子,我们需要计算句子中的单词数
在这里,我们将讨论两种方法-
split()方法test_string = "nhooo.com "
res = len(test_string.split())
print ("The number of words in string are : " + str(res))The number of words in string are : 2
strip()&isalpha()方法import string
test_string = "nhooo.com "
res = sum([i.strip(string.punctuation).isalpha() for i in test_string.split()])
print ("The number of words in string are : " + str(res))The number of words in string are : 2
在本文中,我们了解了对句子中的单词进行计数的方法。