nutship
nutship

Reputation: 4924

removing space from output Python 2.7

elif clocked > limit and clocked <= 90:
    print "Too fast! The fine is $", (clocked - limit) * 5 + 50
else:
    print "Thats was extremely dangerous!\nYour fine is $", \
                                 (clocked - limit) * 5 + 250

Hi, the output for the elif would like: Too fast! The fine is $ 50. I obviously want to get rid of the space between $ 50. I know I can introduce a new variable to assign my expression and then format the variable created with no spacing but is there any way around to skip adding new variable to the code? thanks.

Upvotes: 0

Views: 778

Answers (2)

Jon Clements
Jon Clements

Reputation: 142136

Just change to use string interpolation (although if you're using 2.6+ - it won't hurt to use str.format intead as pointed out by @bobince)

print "Too fast! The fine is $%d" % ((clocked - limit) * 5 + 50,)

Upvotes: 1

bobince
bobince

Reputation: 536349

You don't have to introduce a new variable to use string formatting.

print "Too fast! The fine is ${0}".format((clocked - limit) * 5 + 50)

Upvotes: 1

Related Questions