Home > Blockchain >  Remove space between string and variable when priting
Remove space between string and variable when priting

Time:03-14

First time stackoverflow user. My code:

print("charge  :",currency,("%.2f" % charge))

I am trying to print:

charge : $20.00

However, with the current code it comes out with a space between $ and the variable output. Like this:

charge : $ 20.00

Is there any suggestion on how to resolve this? Apologies if this has already been answered.

CodePudding user response:

The easiest option is to use an f-string:

print(f"charge : {currency}{charge:.2f}")

(Other options include using sep='' to remove the spaces, or using to concatenate the strings yourself, but this makes for code that's harder to read IMO.)

CodePudding user response:

The print() function has an argument that allows you to change the separator of the various elements you print. It is set to a space by default, sep=' ', but it can be anything. So, if you want to remove the space, you can add this this:

print("charge :", currency, ("%.2f" % charge), sep='')
  • Related