HOW TO

Populate a field based on the number of characters in another field in ArcGIS Pro

Last Published: April 2, 2024

Summary

In ArcGIS Pro, the Python len() function can be used in field calculations to count and return the number of characters in a text field. For example, the function can be used in expressions to populate a field with values based on the character count of cells in another field, which can be useful when working with standardized codes or acronyms.

The image below shows the 'Category' field containing acronyms in a feature's attribute table in ArcGIS Pro. In this article, a new field is populated with additional characters if the values in the ‘Category’ field have three or two characters instead of four.

The field with characters to be counted in the attribute table in ArcGIS Pro
Note:
The workflow in this article can only be applied to fields with the text data type, as the len() function is used to count the characters in a string.

Procedure

  1. Open the project in ArcGIS Pro.
  2. In the Contents pane, right-click the feature layer and click Attribute Table.
The Attribute Table button after right clicking the layer in the Contents pane
  1. In the attribute table view, click Add Field to add a new text field.
    1. In the fields view, in the Field Name column, specify a name for the new field. In this example, the field name is ‘Code’.
    2. Under the Data Type column, double-click the cell to open the drop-down menu and click Text.
Adding the new field in the fields view
  1. On the Fields tab, in the Changes group, click Save. Close the fields view.
  1. In the attribute table, right-click the newly added field and click Calculate Field. The ‘Code’ field is used in this example.
The Calculate Field button after right clicking the field in the attribute table
  1. Build an expression using the IF statement and len() function to populate the new field with values based on the number of characters from another field. In the Calculate Field window, configure the following parameters.
    1. For Expression Type, select Python 3.
    2. For Expression, in the text box above the Code Block box, type the following script. Replace <fieldName> with the name of the input field containing characters to be counted. In this example, the input field is the ‘Categories’ field.
calculate_field(!<fieldName>!)
    1. In the Code Block box, type the following script. Replace <field> with a desired variable name, <characters> with the number of characters from the input field to be counted, and <value> with a text value to be added to the new field.
      • Using the if...else statement.
def calculate_field(<field>):
    if (len(<field>) == <characters>):
        return ("<value>" + <field>)
    else:
        return (<field>)
  • Using the if…elif…else statement.
def calculate_field(<field>):
    if (len(<field>) == <characters>):
        return ("<value>" + <field>)
    elif (len(<field>) == <characters>):
        return ("<value>" + <field>)
    else:
        return (<field>)
Note:
The if…elif..else statement is used to include more than a single number of characters from the input field to be counted.

The code block below shows the full working expression using the if…else statement.

def calculate_field(cat):
    if (len(cat) == 3):
        return ("A" + cat)
    else:
        return (cat)

The code block below shows the full working expression using the if…elif…else statement.

def calculate_field(cat):
    if (len(cat) == 3):
        return ("A" + cat)
    elif (len(cat) == 2):
        return ("AA" + cat)
    else:
        return (cat)
Configuring the parameters in the Calculate Field window
  1. Click OK.

The image below shows the ‘Code’ field populated with the additional characters ‘A’ or ‘AA’ if the ‘Category’ field values have three or two characters instead of four.

The new field populated with values based on the number of characters in another field

Article ID: 000032254

Software:
  • ArcGIS Pro 3 1
  • ArcGIS Pro 3 0
  • ArcGIS Pro 3 2

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options