Select to view content in your preferred language

Stacked Label Expression in Python

8968
11
Jump to solution
01-16-2015 05:43 PM
BrynDunbar
Occasional Contributor

I have successfully rendered a stacked label in ArcMap using a java script labeling expression, but I cannot figure out how to do the same thing with Python in the Label Expression box.

 

This is what I used for java script:

 

function FindLabel ( [LOT], [SUBDIVISION] )

{

  return [LOT] +  "\r"  +  [SUBDIVISION];

}

 

How do I do this same thing by using python?

 

I just want to stack the my Lot label on top of my Subdivision label, but using python.

 

Thank you in advance for any thoughts or suggestions.

0 Kudos
11 Replies
BrynDunbar
Occasional Contributor

Thank you so much for taking the time to respond with such great ideas and examples. I will definitely spend more time with label expressions, using these examples to help me construct more complex labels. I am still quite new to coding, but it is quite satisfying when I can understand some code and when I can write code that produces something, especially with no errors.

0 Kudos
DarrenWiens2
MVP Alum

Although it sounds like you've already found an answer to the problem, but you could take advantage of try/except to label, or skip, null values:

def FindLabel ( [LOT], [SUBDIVISION] ):
  try:
    return [LOT] +  "\r"  +  [SUBDIVISION]
  except:
    return 'this had a null value'

Another succinct way to write this is:

def FindLabel ( [LOT], [SUBDIVISION] ):
  if [LOT] and [SUBDIVISION]:
      return [LOT] + '\r' + [SUBDIVISION]
0 Kudos