python统计一段英文语句中的单词数和句子数
时间: 2023-04-27 22:02:06 浏览: 872
可以使用Python编写程序来统计一段英文语句中的单词数和句子数。
首先,需要将英文语句分割成单词和句子。可以使用Python内置的字符串方法split()和splitlines()来分割单词和句子。
例如,以下代码可以将一段英文语句分割成单词列表和句子列表:
```
text = "This is a sample sentence. It contains multiple sentences. Each sentence has multiple words."
words = text.split()
sentences = text.splitlines()
```
接下来,可以使用len()函数来计算单词数和句子数:
```
word_count = len(words)
sentence_count = len(sentences)
```
最后,可以将结果输出:
```
print("单词数:", word_count)
print("句子数:", sentence_count)
```
完整代码如下:
```
text = "This is a sample sentence. It contains multiple sentences. Each sentence has multiple words."
words = text.split()
sentences = text.splitlines()
word_count = len(words)
sentence_count = len(sentences)
print("单词数:", word_count)
print("句子数:", sentence_count)
```
阅读全文