亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

不確定如何從字符串中刪除空格

不確定如何從字符串中刪除空格

忽然笑 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 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號