我有一个程序可以计算并打印句子中包含特定字符的所有单词(忽略大小写)。
Python 代码 -
item=input()
ip=input().tolower()
r=ip.count(item)
print(r)
ip=ip.split()
for word in ip:
if item in word:
print((word), end=' ')
This program works as expected but for the last word that is printed I don't want a white-space after it.
如果有人可以指导我如何删除空间,我将不胜感激。
你为什么不使用列表理解 https://docs.python.org/glossary.html#term-list-comprehension and str.join https://docs.python.org/library/stdtypes.html#str.join?
print(' '.join([w for w in ip if item in w]))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)