How to print +1 in Python, as +1 (with plus sign) instead of 1?

Farshid Palad picture Farshid Palad · Dec 1, 2011 · Viewed 28.4k times · Source

As mentioned in the title, how do I get Python to print out +1 instead of 1?

score = +1
print score
>> 1

I know -1 prints as -1 but how can I get positive values to print with + sign without adding it in manually myself.

Thank you.

Answer

icktoofay picture icktoofay · Dec 1, 2011

With the % operator:

print '%+d' % score

With str.format:

print '{0:+d}'.format(score)

You can see the documentation for the formatting mini-language here.