Unsure how to remove whitespace from strings

Question:

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 produces:

This house is $200000 . The change is $-10000 since last month.
The estimated monthly mortgage is $850.00 .

I am uncertain how to remove the white space after "$200000" and "$850.00". I don’t fully understand the strip() command, but from what I read, it wouldn’t be helpful for this problem.

Asked By: Joshua Beirl

||

Answers:

Maybe try f-string injection

print(f"This house is ${current_price}. The change is ${current_price - last_months_price} since last month.")

f-string (formatted strings) provide a way to embed expressions inside string literals, using a minimal syntax. It’s a simplified way to concatenate strings with out having to explicitly call str to format data types other than strings.

As @Andreas noted below, you could also pass sep='' to print, but that requires you to concatenate other strings with the spaces properly formatted.

Answered By: dubbbdan

You can give print an additional parameter: sep, like this:

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

because the default is an empty space after the comma.

Answered By: Andreas
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))
Answered By: Tyler Grubb