不确定如何从字符串中删除空格

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"我不确定如何删除and之后的空白"$850.00"。我不完全理解这个strip()命令,但从我读到的内容来看,它对这个问题没有帮助。


忽然笑
浏览 133回答 3
3回答

ABOUTYOU

你可以给 print 一个额外的参数:sep,像这样:print("This house is $" + str(current_price), '.', "The change is $" +       str(current_price - last_months_price) + " since last month.", sep='')因为默认是逗号后的空格。

手掌心

也许尝试 f-string 注入print(f"This house is ${current_price}. The change is ${current_price - last_months_price} since last month.")f-string(格式化字符串)提供了一种使用最少语法将表达式嵌入字符串文字的方法。这是一种连接字符串的简化方法,无需显式调用str除字符串以外的格式化数据类型。您也可以传递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))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python