ArcGIS for Desktop

  • Documentation
  • Pricing
  • Support

  • My Profile
  • Help
  • Sign Out
ArcGIS for Desktop

ArcGIS Online

The mapping platform for your organization

ArcGIS for Desktop

A complete professional GIS

ArcGIS for Server

GIS in your enterprise

ArcGIS for Developers

Tools to build location-aware apps

ArcGIS Solutions

Free template maps and apps for your industry

ArcGIS Marketplace

Get apps and data for your organization

  • Documentation
  • Pricing
  • Support
Esri
  • Sign In
user
  • My Profile
  • Sign Out

Help

  • Home
  • Get Started
  • Map
  • Analyze
  • Manage Data
  • Tools
  • More...

Building label expressions

  • Expression examples

You can use label expressions to adjust the formatting of your labels. In addition to inserting characters and scripting functions, you can use ArcGIS formatting tags in label expressions. These are special characters for changing the appearance of all or part of your labels. For example, you might use the bold formatting tag to make the first line bold in a stacked, multiline label.

Learn more about formatting labels with text formatting tags

A label expression is limited to a single line of code unless you check the Advanced box on the Label Expression dialog box. Checking the Advanced box allows you to enter a function containing programming logic and spanning multiple lines of code.

Field values are automatically cast to text strings. Therefore, if you wish to use a numeric value in an arithmetic operation, or when making a comparison, you will need to cast it back to a numeric data type. The examples below add two integer fields:

Python
int([FIELD1]) + int([FIELD2])
VBScript
cint([FIELD1]) + cint([FIELD2])
JScript
parseInt([FIELD1]) + parseInt([FIELD2])

Steps:

  1. Click the Label Manager button Label Manager on the Labeling toolbar.
  2. Click a label class in the Label Classes list.
  3. Click the Expression button.
  4. Choose a language on the Parser menu.
  5. Type a Python, VBScript, or JScript expression. You can also create an expression by double-clicking the field to add it to the expression or by selecting the field and clicking the Append button to append the field to the end of the expression separated by a space.

    Fields are enclosed in square brackets [ ] irrespective of the data type of the layer's data source.

    Optionally, enter ArcGIS text formatting tags in the Expression box to apply formatting to a portion of your label text.

    If your expression will span multiple lines of code, check the Advanced check box and enter your label expression.

  6. Click Verify to make sure there are no syntax errors.
  7. Click OK on each of the dialog boxes.
Tip:
Both regular and advanced label expressions can be saved as label expression files (.lxp), which can be loaded into other layers or maps.

Expression examples

The following are examples of label expressions:

  • Concatenate a string to the value in a field. For example, this expression creates a label where the value of the PARCELNO field is preceded by the text "Parcel no:":
    Python
    "Parcel no: " + [PARCELNO]
    
    VBScript
    "Parcel no: " & [PARCELNO]
    
    JScript
    "Parcel no: " + [PARCELNO]
    
  • Round a decimal number to a set number of decimals. For example, this expression displays an Area field rounded to one decimal place:
    Python
    round(float([AREA]), 1)
    
    VBScript
    Round ([AREA], 1)
    
    JScript
    function FindLabel ( [AREA] )
    {
    var ss;
    var num= parseFloat([AREA]);
    ss =  num.toFixed(1);
      return (ss);
    }
    
  • Convert your text labels to all uppercase or lowercase. For example, this expression makes a Name field all lowercase:
    Python
    def FindLabel ( [NAME] ):
      S = [NAME]
      S = S.lower()
      return S
    
    VBScript
    LCase ([NAME])
    
    JScript
    [NAME].toLowerCase()
    
  • Convert your text labels to proper case. For example, this expression takes a Name field that is in all capitals and makes it proper case:
    Python
    def FindLabel ( [NAME] ):
      S = [NAME]
      S = S.title()
      return S
    
    VBScript
    Function FindLabel ( [NAME] )
    FindLabel = UCase(Left([NAME],1)) & LCase(Right([NAME], Len([NAME]) -1))
    End Function
    
    JScript
    function FindLabel ( [NAME] )
    {
    var str = [NAME];
    var iLen = String(str).length;
    var upper = (str.substring(0,1)).toUpperCase();
    var lower = (str.substring(1, iLen)).toLowerCase()
    return upper + lower;
    }
    
  • Create stacked text. For example, this expression creates a label with the Name field and the two address fields all on separate lines:
    Python
    "Name: " + [NAME] + '\n' + [ADDRESS_1] + '\n' + [ADDRESS_2]
    
    VBScript
    "Name: " & [NAME] & vbCrLf& [ADDRESS_1] & vbCrLf& [ADDRESS_2]
    
    JScript
    "Name: " + [NAME] + "\r" + [ADDRESS_1] + "\r" + [ADDRESS_2]
    
  • Create stacked text based on text from one field. For example, this expression uses the comma to specify where the stack happens:
    Python
    def FindLabel ( [LABELFIELD] ):
      S = [LABELFIELD]
      S = S.replace(', ', '\n')
      return S
    
    VBScript
    Function FindLabel ( [LABELFIELD] )
    FindLabel = replace([LABELFIELD], ", ", vbnewline)
    End Function
    
    JScript
    function FindLabel ( [LABELFIELD] )
    {
    var r, re;
    var str = [LABELFIELD];
    re = /,/g;
    r = str.replace(re, "\r");
    return r;
    }
    
  • Format your labels. For example, this expression displays the label as currency:
    Python
    def FindLabel ( [MAXIMUM_OC], [RATE] ):
      import locale
      locale.setlocale(locale.LC_ALL, '')
      S = locale.currency(float([MAXIMUM_OC]) * float([RATE]))
      return S
    
    VBScript
    "Occupancy Revenue: " & FormatCurrency ([MAXIMUM_OC] * [RATE])
    
    JScript
    function FindLabel ( [MAXIMUM_OC], [RATE] )
    {
    var ss;
    var num1 = parseFloat([MAXIMUM_OC]);
    var num2 = parseFloat([RATE]);
    var num3 = num1 * num2
    ss =  num3.toFixed(2);
      return ("$" + ss);
    }
    
  • Specify a conditional if-else statement. These functions label cities with their name in a large, red font if their population is equal to or exceeds 250,000 and in the default label font if the population is less than 250,000:
    Python
    def FindLabel ( [NAME], [POPULATION] ):
      if long([POPULATION]) >= 250000:
        return "<CLR red='255'><FNT size = '14'>" + [NAME] + "</FNT></CLR>"
      else:
        return [NAME]
    
    VBScript
    Function FindLabel ([NAME], [POPULATION])
      if (cLng([POPULATION]) >= 250000) then
       FindLabel = "<CLR red='255'><FNT size = '14'>" + [NAME] + "</FNT></CLR>"
      else
    	 FindLabel = [NAME]
      end if
    End Function
    
    JScript
    function FindLabel ( [NAME], [POPULATION]  )
    {
    if (parseFloat([POPULATION]) >= 250000){
    return ("<CLR red='255'><FNT size = '14'>" + [NAME] + "</FNT></CLR>");
    }
    else
    return ([NAME]);
    }
    
Note:
To label a subset of features based on a field value, create the SQL query in the label class instead of through the label expression.
  • Python Language Reference
  • Microsoft VBScript Language Reference
  • Microsoft JScript Language Reference

(This information is housed on web pages not created, owned, nor maintained by Esri. Esri cannot guarantee the availability of these pages and is not responsible for the content found on them.)

Related Topics

  • Essential labeling concepts
  • About displaying labels
  • About specifying text for labels
  • Using text formatting tags
Feedback on this topic?

ArcGIS for Desktop

  • Home
  • Documentation
  • Pricing
  • Support

ArcGIS Platform

  • ArcGIS Online
  • ArcGIS for Desktop
  • ArcGIS for Server
  • ArcGIS for Developers
  • ArcGIS Solutions
  • ArcGIS Marketplace

About Esri

  • About Us
  • Careers
  • Insiders Blog
  • User Conference
  • Developer Summit
Esri
© Copyright 2016 Environmental Systems Research Institute, Inc. | Privacy | Legal