如何在Python中删除空格

2024-10-08 18:30:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要把我的句子放在一行上,但我不知道怎么说。我尝试了.string(),但不确定是否正确使用了它。 我的代码是:

def printCurrency(value):
    print("$" + format(value, '.2f'))

print("That is" end=" ")
printCurrency(cost)
print("for this service")

它是这样打印出来的

那是22美元

为了这项服务。你知道吗

谢谢


Tags: 代码formatforstringthatisvaluedef
2条回答

只要把printCurrency改成formatCurrenty就可以了

def formatCurrency(value):
    return "$" + format(value, '.2f')  # don't print the value but return it

print("That is %s for this service" % formatCurrenty(cost))

对于这个特定问题,我将使用formatCurrency解决方案。如果你的问题比较笼统:

要在python2.x中使用print而不获取换行符,请在末尾添加逗号。你知道吗

例如

>>> def f():
...     print 'hello',
...     print 'world'
... 
>>> f()
hello world

在python3.x中,只需设置end=''

>>> def f():
...     print('hello ', end='')
...     print('world')
... 
>>> f()
hello world

相关问题 更多 >

    热门问题