如何在Python中删除空格

如何在Python中删除空格,python,eclipse,Python,Eclipse,我需要把我的句子放在一行上,但我不知道怎么说。我尝试了.string(),但不确定是否正确使用了它。 我的代码是: def printCurrency(value): print("$" + format(value, '.2f')) print("That is" end=" ") printCurrency(cost) print("for this service") 它是这样打印出来的 那是22美元 为了这项服务 谢谢只需将printCurrency更改为formatCurr

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

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

print("That is" end=" ")
printCurrency(cost)
print("for this service")
它是这样打印出来的

那是22美元

为了这项服务


谢谢

只需将
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))

只需将
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
而不获取换行符,请在末尾添加逗号

e、 g

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


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

要在Python2.x中使用
print
而不获取换行符,请在末尾添加逗号

e、 g

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


您可以为
printCurrency()
函数添加代码吗?问题在于函数
printCurrency(cost)
,而不是在此代码中。与其使用
printCurrency
函数,不如使用
formatCurrency
函数返回字符串
printCurrency
将要打印的字符串。@user2357112 hm,你比我快19秒。你为什么不把你的评论作为答案发布呢?我添加了printCurrency的代码。拥有formatCurrency函数是什么意思?能否为
printCurrency()
函数添加代码?问题出在函数
printCurrency(cost)
中,而不是在此代码中,而不是拥有
printCurrency
函数,使用
formatCurrency
函数返回本应打印的字符串
printCurrency
可能更容易。@user2357112 hm,您比我快19秒。为什么不将您的评论作为答案发布?我为printCurrency添加了代码。拥有formatCurrency函数是什么意思?请注意,end仅适用于当前打印调用。请注意,end仅适用于当前打印调用。
>>> def f():
...     print('hello ', end='')
...     print('world')
... 
>>> f()
hello world