Python 不确定如何从字符串中删除空白
这将产生:Python 不确定如何从字符串中删除空白,python,python-3.6,removing-whitespace,Python,Python 3.6,Removing Whitespace,这将产生: current_price = int(input()) last_months_price = int(input()) print("This house is $" + str(current_price), '.', "The change is $" + str(current_price - last_months_price) + " since last month.") print(&quo
current_price = int(input())
last_months_price = int(input())
print("This house is $" + str(current_price), '.', "The change is $" +
str(current_price - last_months_price) + " since last month.")
print("The estimated monthly mortgage is ${:.2f}".format((current_price * 0.051) / 12), '.')
我不确定如何删除$200000和$850.00之后的空白。我不完全理解strip命令,但从我读到的内容来看,它对解决这个问题没有帮助。也许可以尝试f-string注入 这房子的价格是${现行价格}。更改为${current_price-last_months_price}自上个月以来的${current_price-last_months_price}。 格式化字符串提供了一种在字符串文本中嵌入表达式的方法,使用最小语法。这是一种连接字符串的简化方法,无需显式调用str来格式化字符串以外的数据类型
正如@Andreas在下面指出的,您也可以将sep=传递给print,但这需要您将其他字符串与正确格式化的空格连接起来。您可以为print提供一个附加参数:sep,如下所示:
This house is $200000 . The change is $-10000 since last month.
The estimated monthly mortgage is $850.00 .
因为默认值是逗号后的空白。删除、?使用f字符串,比如printft这房子是${current_price}。更改为${current\u price-last\u months\u price}。第一个是句子估计的…,第二个是。。如果输入多个参数,则它们之间用空格分隔。修复方法是将点放在第一个参数中:…抵押贷款为${:.2f}>>。将删除尾随的空白字符。@martineau本例中的空白来自打印,而不是字符串,否则您是对的。所以您的解决方案有效,但您能解释一下原因吗?什么是f串注入?非常感谢你@JoshuaBeirl更新了。f字符串创建非常简洁易读的代码行。
print("This house is $" + str(current_price), '.', "The change is $" +
str(current_price - last_months_price) + " since last month.", sep='')