HOW TO

Remove leading text from a string using Python or Arcade in ArcGIS Pro

Last Published: January 30, 2024

Summary

In ArcGIS Pro, Python or Arcade expressions can be used to create a new field without the unnecessary text strings of a field. This is common in address records whereby the street number is not required for analysis. Removing the leading text from a string can be used for data management or search results optimization.

This article provides an example and instructions using Python and Arcade expressions to remove leading text from a string in ArcGIS Pro.

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

The Schools_all attribute table with the ADDRESS 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. 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:
  • For Python 3:
    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, 'PYTHONSPLITADDRESS' is created.
    3. For Expression Type, select Python 3.
    4. Specify the following script in the first text block. The 'maxSplitParameter' is the number of split elements. As an example, setting the 'maxSplitParameter' to 1, returns a list with two elements. The 'indexNumber' refers to an element of an iterable object by its position within the iterable object. As an example, setting the 'indexNumber' to 1, returns the second element.
!<fieldName>!.strip().split(" ",<maxSplitParameter>)[<indexNumber>]

The code block below is an example of the expression.

!ADDRESS!.strip().split(" ",1)[1]
  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
  • For Arcade expressions:
    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, 'ARCADESPLITADDRESS' is created.
    3. For Expression Type, select Arcade.
    4. Specify the following Arcade expression in the Expression section:
      1. Define the variable to find a sequence of characters within a text value. In this example, 'varName1' is named as fstSpace.
var <varName1> = find(' ',$feature.<fieldName>,<startPosition>)
  1. Define the variable to count the number of characters in a text value. In this example, 'varName2' is named as strCount.
var <varName2> = Count($feature.<fieldName>)
  1. Define the variable to calculate the difference between 'varName1' and 'varName2'. In this example, 'varName3' is named as strDiff.
var <varName3> = <varName2> - <varName1>
  1. Specify the following statement to return the specified number of characters from the end of a text value.
return right($feature.<fieldName, <varName3>)

The code block below is an example of the full expression.

var fstSpace = Find(' ', $feature.ADDRESS, 0)
var strCount = Count($feature.ADDRESS)
var strDiff = strCount - fstSpace

return Right($feature.ADDRESS, strDiff)
  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 'PYTHONSPLITADDRESS' and 'ARCADESPLITADDRESS' fields added to the 'Schools_all' attribute table with the leading text removed from the 'ADDRESS' field using Python and Arcade expressions.

The Schools_all attribute table with two new fields added

Article ID: 000029050

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