89

Is there a better way to print the + sign of a digit on positive numbers?

integer1 = 10
integer2 = 5
sign = ''
total = integer1-integer2
if total > 0: sign = '+'
print 'Total:'+sign+str(total)

0 should return 0 without +.

3 Answers 3

106

Use the new string format

>>> '{0:+} number'.format(1)
'+1 number'
>>> '{0:+} number'.format(-1)
'-1 number'
>>> '{0:+} number'.format(-37)
'-37 number'
>>> '{0:+} number'.format(37)
'+37 number'
# As the questions ask for it, little trick for not printing it on 0
>>> number = 1
>>> '{0:{1}} number'.format(number, '+' if number else '')
'+1 number'
>>> number = 0
>>> '{0:{1}} number'.format(number, '+' if number else '')
'0 number'

It's recommended over the % operator

5
  • 1
    0 -> +0, not what the OP wants. May 4, 2010 at 7:41
  • Yes, sorry, I've noticed the moment I hit the button. I've corrected it.
    – Khelben
    May 4, 2010 at 7:46
  • 2
    Nice answer! You don't need to explicitly index the components inside format(), so {:+} and {:{}} are sufficient. They also make better emoticons.
    – 1''
    Aug 11, 2015 at 21:34
  • Would sill print the sign for -0.0.
    – Dan M.
    May 8, 2018 at 16:37
  • 4
    It turns out f"strings" also allow nested replacement fields, so f"{number:{'+' if number else ''}}" also works.
    – SE is dead
    Mar 6, 2020 at 4:45
70
>>> print "%+d" % (-1)
-1
>>>
>>> print "%+d" % (1)
+1
>>> print "%+d" % (0)
+0
>>>

Here is the documentation.

** Update** If for whatever reason you can't use the % operator, you don't need a function:

>>> total = -10; print "Total:" + ["", "+"][total > 0] + str(total)
Total:-10
>>> total = 0; print "Total:" + ["", "+"][total > 0] + str(total)
Total:0
>>> total = 10; print "Total:" + ["", "+"][total > 0] + str(total)
Total:+10
>>>
4
  • Thanks for your answer. I'm working with web.py templator and i'm not sure if i can use the string formatting % operator. Is there a Python method that return the sign of a given number? May 4, 2010 at 7:21
  • 3
    Huh? You used the % operator in your question! Please edit your question so that it reflects your real requirement(s). May 4, 2010 at 7:27
  • ("+" if total > 0 else "") is 6 more characters, but a bit more direct and (IMHO) clearer.
    – jchl
    Jul 5, 2010 at 10:31
  • how can we do 0 should return 0 without +? print "Total:" + ["", ""][total > 0] + str(total)?
    – Lumos
    Oct 24, 2017 at 7:02
41

From python 3.6 onwards:

>>> integer1 = 10
>>> integer2 = 5
>>> total = integer1-integer2
>>> print(f'Total: {total:+}')
Total: +5

or:

for i in range(-1,2):
    print (f' {i} becomes {i:+}')

outputs:

 -1 becomes -1
 0 becomes +0
 1 becomes +1
3
  • I note the 0 returns +0 but personally I prefer this. Sep 7, 2018 at 2:40
  • 1
    How would you do this with float numbers? Feb 7, 2020 at 16:01
  • 6
    f"{123.456:+.2f}" => '+123.46' Jul 27, 2020 at 19:13

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.