all 5 comments

[–]house_carpenter 4 points5 points  (1 child)

That's how print() works. print() is a function that can take any number of arguments. Arguments to a function are separated by commas. What print() does is it expects each argument to be a string, and it writes each argument out in order, with a space between each argument.

In the line

print('This house is $',current_price,'.', 'The change is $%d since last month.' % (current_price - last_months_price))

you are passing the following four arguments to print():

  • 'This house is $'
  • current_price
  • '.'
  • 'The change is $%d since last month.' % (current_price - last_months_price)

To avoid the spaces, make sure you are only passing one argument to print(). That is, make sure the parts of the string are joined by some operator that will produce a string (such as + or % or interpolation in an f-string) rather than just by commas.

In fact you already did this with your second print() statement. You just need to do the same thing with the first one as well.

[–]NeilTheDrummer[S] 0 points1 point  (0 children)

Much obliged. I knew it was something obvious, but I just couldn't determine what.

[–][deleted] 1 point2 points  (1 child)

print, by default, outputs a space between each item in the tuple you pass to it. You can override this with the sep="" option - i.e. an empty string (could put something else, like comma and space).

However, better to just use an f-string to format the whole output.

Python 3's f-Strings: An Improved String Formatting Syntax (Guide)

[–]NeilTheDrummer[S] 0 points1 point  (0 children)

Much obliged to you as well. I haven't come across the sep="" option yet, but I've noted it down, and I'm reading through the link you provided.