1、输入一个至少有10个英文单词构成的字符串,如:We love School of Computer Science and Engineering Yulin Normal University!要求: (1)统计以字母w开头的单词数; (2)统计单词中含“o”字符串的单词数; (3)统计长度为3的单词数。
时间: 2024-02-15 11:01:28 浏览: 142
输入一个字符串,统计出这个字符串的英文大写字母、小写字母、数字、空格以及其它字符的个数
5星 · 资源好评率100%
好的,我来回答您的问题:
1. 统计以字母w开头的单词数:
```python
sentence = "We love School of Computer Science and Engineering Yulin Normal University!"
words = sentence.split() # 将句子分割成单词
count = 0
for word in words:
if word.startswith('w') or word.startswith('W'): # 判断单词是否以W或w开头
count += 1
print("以W开头的单词数为:", count)
```
2. 统计单词中含“o”字符串的单词数:
```python
sentence = "We love School of Computer Science and Engineering Yulin Normal University!"
words = sentence.split() # 将句子分割成单词
count = 0
for word in words:
if 'o' in word or 'O' in word: # 判断单词中是否含有o或O
count += 1
print("含有o的单词数为:", count)
```
3. 统计长度为3的单词数:
```python
sentence = "We love School of Computer Science and Engineering Yulin Normal University!"
words = sentence.split() # 将句子分割成单词
count = 0
for word in words:
if len(word) == 3: # 判断单词长度是否为3
count += 1
print("长度为3的单词数为:", count)
```
希望能够帮到您!
阅读全文