Python Forum

Full Version: Removing extra space
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The print output annoys me. Big Grin
My question is how do I get rid of the space betwen the the dollar sign and value.
I did try to search the web, but couldnt find a solution.

I am running this in Centos 7 Python 2.7.5
Output is given below, then the code.

Quote:How many copies are you buying ? 25
Unit price: $ 89
Total price: $ 2225


from __future__ import print_function
import sys, os
def cls():
    os.system('clear')

def main():
    cls()
    copur = int(raw_input('How many copies are you buying ? '))
    if copur <= 10:
         uprice = 99
    elif copur > 10 and copur <= 50:
         uprice = 89
    elif copur > 50 and copur <= 100:
         uprice = 79
    elif copur > 100:
         uprice = 69
    #tot = float(copur * uprice)
    tot = int(copur * uprice)
    print('Unit price: $',uprice)
    print('Total price: $',tot)
This happens because in the print function you are using commas. This should fix it:

    
print('Unit price: $' + str(uprice))
print('Total price: $' + str(tot))
I prefer the format method of strings:

print('Unit price: ${}'.format(uprice))
print('Total price: ${}'.format(tot))
You should switch to Python 3 if possible. At the end of the year support for 2.7 will stop.
Alternatively you could make use of the optional sep parameter of the print function:
print(1,2,3, sep='---')
print(1,2,3, sep='')
Output:
1---2---3 123
Thanks ....