In Python, how do I remove space?
执行代码后,我得到以下打印输出:
"Het antwoord van de berekening is: 8775 ."
当我想得到"Het Antwoord van de Berekening是:8775。"。所以我想去掉数字和圆点之间的空格。我该怎么做?
1 2 3 4 5 6 7 8 | Berekening1 = 8.5 Berekening2 = 8.1+4.8 Berekening3 = 8*10 Berekening4 = 3 x = Berekening1 * Berekening2 * Berekening3 + Berekening4 print"Het antwoord van de berekening is:", print int(x), print"." |
不要使用
1 | print"Het antwoord van de berekening is: {}.".format(x) |
在这里,
1 2 3 | >>> x = 42 >>> print"Het antwoord van de berekening is: {}.".format(x) Het antwoord van de berekening is: 42. |
您也可以使用字符串连接,但这更麻烦:
1 | print"Het antwoord van de berekening is:" + str(x) +"." |
你可以使用:
1 | print"Het antwoord van de berekening is: {}.".format(x) |
您不想使用python 3吗?在python 3中,
1 2 3 4 5 6 7 8 9 10 11 12 13 | In [1]: help(print) Help on built-in function print in module builtins: print(...) print(value, ..., sep=' ', end=' ', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream. |
根据Padraic的评论,这是如何应用于你的问题的(或者说,老实说,是你对成为你问题的任务的特定方法)?你有两种可能
1 2 3 4 5 6 7 | In [2]: print('The result is ', 8775, '.', sep='') The result is 8775. In [3]: print('The result is ', end=''); print(8755, end=''); print('.') The result is 8775. In [4]: |
如果您困在python 2中,您仍然可以利用
1 | from __future__ import print_function |
在你的程序中。
如果你对这些东西不太了解,你的朋友也是……,