如何删除python输出末尾的空格? [英] How do I remove space at the end of an output in python?
本文介绍了如何删除python输出末尾的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个程序可以计算并打印包含特定字符(忽略大小写)的句子中的所有单词.
I have a program that counts and prints all words in a sentence that contains a specific character(ignoring case).
Python 代码 -
Code in 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.
如果有人能指导我如何删除空间,我将不胜感激.
If anyone could guide me on how to remove the space it would be appreciated.
推荐答案
Why don't you use list comprehension and str.join
?
print(' '.join([w for w in ip if item in w]))
这篇关于如何删除python输出末尾的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文