用python编写程序,用户输入一段英文,然后输出这段英文中所有长度为三个字母的单词
时间: 2023-05-21 08:03:10 浏览: 198
可以使用正则表达式来匹配长度为三个字母的单词,以下是示例代码:
```python
import re
text = input("请输入一段英文:")
pattern = r'\b[a-zA-Z]{3}\b'
matches = re.findall(pattern, text)
print(matches)
```
用户输入一段英文后,程序会输出这段英文中所有长度为三个字母的单词。
相关问题
用python编写一个 用户输入一段英文,输出这段英文中所有长度为三个字母的单词
你好,我是 C 知道。以下是回答:
可以使用正则表达式来实现这个功能。具体代码如下:
import re
text = input("请输入一段英文:")
pattern = r'\b\w{3}\b'
result = re.findall(pattern, text)
print(result)
这段代码中,我们首先使用 input() 函数来获取用户输入的英文文本。然后,我们使用正则表达式 r'\b\w{3}\b' 来匹配所有长度为三个字母的单词。其中,\b 表示单词的边界,\w 表示任意一个字母或数字。最后,我们使用 re.findall() 函数来查找所有匹配的单词,并将结果打印出来。
希望这个回答能够帮到你!
用python编写程序,不用正则表达式,用户输入一段英文,然后输出这段英文中所有长度为三个字母的单词
可以使用以下代码实现:
```python
text = input("请输入一段英文:")
words = text.split()
result = [word for word in words if len(word) == 3]
print(result)
```
用户输入一段英文后,程序将其分割成单词,并筛选出长度为三个字母的单词,最后输出结果。
阅读全文
相关推荐











