1

Currently, I'm having an error, where

w = float(input("Enter weight(grams): "))
print (w/1000,'kg')

returns

Enter weight(grams): 3000
3.0 kg

When attempting to convert from grams to kg in something. Problem is the space between the 3.0 and the kg which I cannot seem to find an answer for. I really dislike the spacing default within the print function and .strip() doesn't seem to be working for me.

My desired result is

Enter weight(grams): 3000
3.0kg
Sundrah
  • 785
  • 1
  • 8
  • 16

2 Answers2

1

it's because when you give multiple values to the print() function, it seperates all the expressions with space. You should use a string format instead:

>>> w = 3000
>>> print ("{}{}".format(w/1000, 'kg'))
3.0kg

Another, less clean way to do it, is to give the sep parameter to print() so it's set no nothing instead of the default space:

>>> w = 3000
>>> print (w/1000, 'kg', sep='')
3.0kg

But always prefer the string format, it's more flexible and powerful.

zmo
  • 24,463
  • 4
  • 54
  • 90
1

You can specify a seperator for the print function.

>>> print("Hello", "World", sep='')
HelloWorld