HOW TO

Extract the leading, middle and ending string from a text field in ArcGIS Pro

Last Published: February 28, 2024

Summary

In ArcGIS Pro, the leading, middle, and ending text can be extracted from a string field using the Python parser in the Calculate Field tool. This is useful for data management, better readability, and identifying unique identifiers. For instance, when dealing with datasets containing detailed location descriptions in text fields, extracting different parts of the text can simplify spatial querying and classification tasks.

This article provides the workflow to extract the leading, middle, and ending text from a string field using the Python parser in the Calculate Field tool to populate them in another field. In this example, the ending text is extracted from a field named Location_Description.

Procedure

  1. Open the ArcGIS Pro project.
  2. In the Contents pane, right-click the feature layer and click Attribute Table.
  3. Create a new text field type to store the extracted characters. In this example, the new field is named Ext_Description.
  4. In the attribute table, right-click the field name, and select Calculate Field.
  5. In the Calculate Field window, specify the following parameters:
    1. For Input Table, ensure the layer from Step 2 is selected. In this example, the Test layer is used.
    2. For Field Name, ensure the field created in Step 3 is selected..
    3. For Expression Type, select Python.
    4. For Expression, in the first expression box, specify the code provided below. Replace Field_name with the name of the field containing the values to be extracted. In this example, the field name used is Location_Description.
extract_characters(!Field_Name!)
  1. In the Code Block box, specify the following expression.
    • Use the code below to extract the leading text from a string.
def extract_characters(sentence):
    space_index = sentence.find(' ')
    first_word = sentence[:space_index] if space_index != -1 else sentence
    num_characters = len(first_word)
    extracted_characters = first_word[:num_characters]
    return extracted_characters
    • Use the code below to extract the middle text from a string.
def extract_characters(sentence):
    space_index = sentence.find(' ')
    last_space_index = sentence.rfind(' ')
    middle_word = sentence[space_index + 1:last_space_index] if space_index != -1 and space_index != last_space_index else sentence
    num_characters = len(middle_word)
    extracted_characters = middle_word[:num_characters]
    return extracted_characters
    • Use the code below to extract the ending text from a string.
def extract_characters(sentence):
    space_index = sentence.rfind(' ')
    last_word = sentence[space_index + 1:] if space_index != -1 else sentence
    num_characters = len(last_word)
    extracted_characters = last_word[:num_characters]
    return extracted_characters
  1. Click the Verify icon to validate the expression.
  2. Click OK to run the script.
Python script configuration in the Calculate Field window.png

The image below displays the Ext_Description field in the attribute table containing the ending text extracted from the Location_Description field.

Field displaying extracted last word.png

Article ID: 000031954

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