How To: Add leading zeros to values in a field in an attribute table
Instructions provided describe how to add leading zeros to values in a number or text field in an attribute table.
A desired number of digits must be set for the field before adding the leading zeros. For example, for a field containing numbers from 0 to 200, and the desired number of digits is set to be 3, value one is displayed as 001, value 99 is displayed as 099, and value 200 is displayed as 200. This allows all the values in the field to consist of a same number of digits.
- Add a feature class or shapefile to ArcMap.
- Right-click the layer in the Table Of Contents > Edit Features > Start Editing.
- Open the attribute table by right-clicking the layer in the Table Of Contents > Open Attribute Table.
- Click Table Options > Add Field.
- In the Add Field window, name the field, Select Text in the Type drop-down menu, and specify the desired field length.
- Populate the newly created field with the desired values. For information related to populating a field using the Field Calculator tool, refer to ArcMap: Fundamentals of field calculations and ArcMap: Using the Calculate Field tool.
- Right-click the newly created field > Field Calculator.
- In the Field Calculator window, select Python as the parser.
- Under the Fields section, double-click the newly created field.
- Under the Type section, select String.
- In the Functions section, scroll-down the list and double-click .zfill().
- Specify the desired number of digits in the brackets of .zfill(). The following is a sample expression:
- Click OK.