-1

I want to retain the leading "00" when a value is incremented by one,below val has "00349",when i increment and print it becomes 350,how to print as 00350 retaining the leading "00"

val = 00349
val = val + 1
print val -->prints as 350,i want to print as 00350
user3654069
  • 345
  • 2
  • 6
  • 14
  • It probably is, if you know what to look for. For people starting to program, "padding" might be the word they're missing. – mrks Jun 04 '14 at 16:35
  • Also note that this code breaks in python2. A number starting in `0` is considered in octal, and you have a 9 that is impossible. `val = 0034; print val --> 28` – Davidmh Jun 04 '14 at 16:36

1 Answers1

3

You will need to print the number using a format string:

print "%05d" % val

Also, be aware that by storing a number with a leading zero, you store it as an octal number:

>>> val = 0123
>>> print val
83
mrks
  • 8,033
  • 1
  • 33
  • 62