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), '.')
这产生:
This house is $200000 . The change is $-10000 since last month.
The estimated monthly mortgage is $850.00 .
我不确定如何去除"$200000"
和"$850.00"
之后的空白。我不完全理解strip()
命令,但从我读到的内容来看,它对这个问题没有帮助。
您可以为print提供一个附加参数:sep
,如下所示:
print("This house is $" + str(current_price), '.', "The change is $" +
str(current_price - last_months_price) + " since last month.", sep='')
因为默认值是逗号后面的空白。
也许可以尝试f字符串注入
print(f"This house is ${current_price}. The change is ${current_price - last_months_price} since last month.")
f-string(格式化字符串(提供了一种使用最小语法将表达式嵌入字符串文本中的方法。这是一种连接字符串的简化方法,不必显式调用str
来格式化字符串以外的数据类型。
正如@Andreas在下面指出的,您也可以将sep=''
传递给print
,但这需要您将其他字符串与格式正确的空格连接起来。
print("This house is $" + str(current_price), '.',' ' "The change is $" +
str(current_price - last_months_price) + " since last month.", sep='')
print("The estimated monthly mortgage is ${:.2f}"'.'.format((current_price * 0.051) / 12))