Python程序来计算句子中的单词
在本文中,我们将学习解决给定问题陈述的解决方案和方法。
问题陈述
给我们一个句子,我们需要计算句子中的单词数
在这里,我们将讨论两种方法-
方法1-使用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
方法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
结论
在本文中,我们了解了对句子中的单词进行计数的方法。
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短