首页 > 解决方案 > 如何在 Python 中删除打印语句中的最后一个空格和换行符?

问题描述

我有一个任务,如果我们使用的 if 语句失败,它的一部分要求我们打印一个语句。但是,我们不应该在打印的语句之后包含空格或换行符。

if second < first:
    print('Second integer can\'t be less than the first.',end='')

将摆脱新行,但它仍然说最后有一个空格。我找不到任何说明如何摆脱最后空间的文档。它打印这个

'Second integer can't be less than the first. '

应该打印时带有空格

'Second integer can't be less than the first.'

没有空格。

标签: python

解决方案


如果再添加一条打印语句,您会看到句号后面没有空格。

print('Second integer can\'t be less than the first.',end='')
print ('<----')

推荐阅读