Python Script to Automate Zip Codes Help

06-20-2017 10:10 AM
New Contributor

So I am dealing with zip codes and when the zip code starts with 0, excel does not keep and when I import into arcmap, I therefore have issues joining to other shapefiles with a zip-code field. I am trying to write a script that will automate this process and calculate a new field to ensure that all zip codes have 5 digits but I cant get it to work. Attached is what I have written in python, essentially to determine the length of the zip code, and if it is complete (5digits) just make I a string and if it is not, to concatenate the necessary number of 0s to make it 5 digits.

0 Kudos
3 Replies
MVP Esteemed Contributor

Just looking at your expression, and not addressing the script, what you want can be easily accomplishing using Python string formatting:


Also, this will get more visibility if posted in‌.

Esri Esteemed Contributor

In addition to the solution provided by Joshua Bixby , you can find some more examples of doing this in Some Python Snippets (see the part of Formatting leading zero's in General Snippets).

MVP Esteemed Contributor

Then there is the blogs on Basic Fancy Formatting... with references to Fancy Fancy Formatting and a variety of other tips.  You can waste a whole day making stuff look nice without getting any meaningful work done... all the tips are there