首页 > 试题广场 >

编写程序,用户输入一段英文,然后输出这段英文中所有长度为3个

[问答题]

编写程序,用户输入一段英文,然后输出这段英文中所有长度为3个字母的单词。

这里给出Python 3.4.2代码,如果使用Python 2.7.8的话只需要修改其中的print()函数为print语句即可。

import re
x = input('Please input a string:')
pattern = re.compile(r'\b[a-zA-Z]{3}\b')
print(pattern.findall(x))

发表于 2017-12-28 15:45:59 回复(0)