【发布时间】:2017-11-06 19:56:57
【问题描述】:
我需要通过在Python中定义一个函数来计算下面所示文本的单词数和空格数。我尝试使用split(" ") 命令,但由于文本位于两行不同的行中,我不知道该怎么做。有人可以帮帮我吗?
text1 = "A sentence is a group of words that makes complete sense, \n
contains a main verb, and begins with a capital letter."
【问题讨论】:
-
@ChristianDean 示例中没有换行符...
-
@ChristianDean
'1\n3 2'.split()结果为['1', '3', '2']。我在这里错过了什么吗? -
@alfasin 是的,我意识到这一点。我只是按照OP所说的去做。
-
另见字符串方法
splitlines()。
标签: python count counter words