FindLabel not Defined in ArcMap labeling with Python Parser?

I think there is a bug in their parser as I am not getting identical behavior between a Python prompt and the label expression evaluator.

Try the following as a workaround:

def FindLabel ( [Direction] ):
    return "{}° {}' {}".format(*[Direction].split('-')) + chr(34)

There were two problems:

  1. Due to a bug with the ESRI parser (this is legal in pure Python), you cannot escape a double-quote with a backslash if the string is surrounded by double-quotes. Similarly, you cannot escape a single-quote with a backslash if the string is surrounded by single-quotes, although this is also legal in pure Python. Triple-quoted strings also don't work the same as at a Python prompt.

    Examples that work in a Python prompt but not in a Python label expression:

    • "I am 6'2\" tall."
    • """I am 6'2" tall."""
    • 'I am 6\'2" tall.'
    • '''I am 6'2" tall.'''

    To work around this, you can instead concatenate it or format it into the string separately.

  2. The str.format() function expects args or kwargs, not a sequence. You can use the * operator to unpack a sequence into arguments, which is what I did above, and it works.


Side note -- because this is a 1-liner you can uncheck the "Advanced" box and just use the expression itself instead of a complete function:

"{}° {}' {}".format(*[Direction].split('-')) + chr(34)