new
new

Reputation: 79

How do I remove spaces in between my output?

This is my code:

cost = input("Enter the expenses: ")
cost = cost.split()
total = sum([int(i) for i in cost])
print("Total: $",total)

This is the output:

Enter the expenses: 5 6 89 5
Total: $ 105

I just need help to remove the space after the $ sign.

Upvotes: 0

Views: 84

Answers (2)

Anand S Kumar
Anand S Kumar

Reputation: 91007

If you want to do it from print() , you can use sep argument. Example -

print("Total: $",total,sep='')

By default (if no sep parameter is specified) Python uses ' ' (space) as sep, to separate each different argument to print() function , and that is why you get a space inbetween your $ and total . Using above method we change that to an empty strig.

Demo -

>>> total = 123
>>> print("Total: $",total,sep='')
Total: $123

Or you can use str.format that would give you more control on formatting your output. Example -

print("Total: ${}".format(total))

Upvotes: 3

xiº
xiº

Reputation: 4687

print("Total: $" + str(total))

Upvotes: 2

Related Questions