为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

忽然笑 2023-06-20 10:37:00
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()命令,但从我读到的内容来看,它对这个问题没有帮助。
查看完整描述

3 回答

?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

你可以给 print 一个额外的参数:sep,像这样:

print("This house is $" + str(current_price), '.', "The change is $" + 
     str(current_price - last_months_price) + " since last month.", sep='')

因为默认是逗号后的空格。


查看完整回答
反对 回复 2023-06-20
?
手掌心

TA贡献1942条经验 获得超3个赞

也许尝试 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,但这需要您将其他字符串与格式正确的空格连接起来。


查看完整回答
反对 回复 2023-06-20
?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

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))



查看完整回答
反对 回复 2023-06-20
  • 3 回答
  • 0 关注
  • 100 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信