Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing extra space
#1
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)
Reply
#2
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))
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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
Reply
#5
Thanks ....
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to remove extra space from output list? longmen 3 1,760 May-05-2022, 11:04 PM
Last Post: longmen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020