How to print +1 in Python, as +1 (with plus sign) instead of 1?
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.
for python>=3.8+
score = 0.2724
print(f'{score:+g}') # or use +f to desired decimal places
# prints -> +0.2724
percentage
score = 0.272425
print(f'{score:+.2%}')
# prints -> +27.24%