HOW TO

Split and rearrange text strings within a field in ArcGIS Pro

Last Published: December 15, 2022

Summary

In ArcGIS Pro, Python expressions with multiple statements can be used to split and rearrange text strings of a field. The rearranged text strings can be used for data management or search results optimization. This article provides an example and instructions using Python expressions to split and rearrange text strings of a field for ArcGIS Pro.

The image below shows the Forest_Name field with the text string records.

The ForestBoundaryFullName attribute table with the Forest_Name field

Procedure

  1. In the Contents pane, right-click the feature class and click Attribute Table to open the attribute table of the feature class.
Opening the attribute table of the feature class from the Contents pane
  1. Highlight the field where the records must be changed.
  2. Click Calculate to open the Calculate Field tool.
Note:
Refer to ArcGIS Pro: Calculate Field (Data Management) for more information on the parameters in the Calculate Field tool.
  1. In the Calculate Field dialog box, configure the following parameters:
    1. For Input Table, select the feature class.
    2. For Field Name (Existing or New), select the field to be edited or create a new field. In this example, Sort_Forest_Name is created.
    3. For Expression Type, select Python 3.
    4. In the first text block, specify the following script:
<function_name>(!<feature_name>!)
  1. Specify the following Python 3 expression in the Code Block section:
    1. Define the function to be called in Step 4(d) in the field to split and create a list of names. In this example, the function is named "sort_name()".
def sort_name(n):
    split = n.split()
  1. Specify the following statement to return a blank value when the field value is empty.
    if len(split) == 0:
        return ''
  1. Specify the following statement to return the same text value when the field is a single word.
    if len(split) < 2:
        return ('{0}').format(split[0])
  1. Specify the following statement to return the text value if the length of the field value is less than three that must be rearranged.
    elif len(split) < 3:
        return ('{0} {1}').format(split[1], split[0])
    else:
        return ('{0} {1} {2}').format(split[1], split[2], split[0])
The code block below demonstrates the full expression.
def sort_name(n):
    split = n.split()

    if len(split) == 0:
        return ''

    if len(split) < 2:
        return ('{0}').format(split[0])

    elif len(split) < 3:
        return ('{0} {1}').format(split[1], split[0])
    else:
        return ('{0} {1} {2}').format(split[1], split[2], split[0])
  1. Click Verify to run a test of the expression. If the expression is valid, click OK.
The Calculate Field tool pane with the parameters to be filled

The image below shows the ForestBoundaryFullName attribute table with the updated field values and the name text rearranged.

The ForestBoundaryFullName attribute table with the updated field values with the name text rearranged

Article ID: 000028839

Software:
  • ArcGIS Pro 3 0
  • ArcGIS Pro 2 8 x
  • ArcGIS Pro 2 x

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