<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Stacked Label Expression in Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334547#M26103</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 23 Jan 2015 22:37:07 GMT</pubDate>
    <dc:creator>BrynDunbar</dc:creator>
    <dc:date>2015-01-23T22:37:07Z</dc:date>
    <item>
      <title>Stacked Label Expression in Python</title>
      <link>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334538#M26094</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I used for java script:&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;function FindLabel ( [LOT], [SUBDIVISION] )&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp; return [LOT] +&amp;nbsp; "\r"&amp;nbsp; +&amp;nbsp; [SUBDIVISION];&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I do this same thing by using python?&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I just want to stack the my Lot label on top of my Subdivision label, but using python.&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance for any thoughts or suggestions.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 17 Jan 2015 01:43:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334538#M26094</guid>
      <dc:creator>BrynDunbar</dc:creator>
      <dc:date>2015-01-17T01:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked Label Expression in Python</title>
      <link>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334539#M26095</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It is very similar:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def FindLabel ( [LOT] , [SUBDIVISION]&amp;nbsp; ):
&amp;nbsp; return [LOT] +&amp;nbsp; "\n"&amp;nbsp; +&amp;nbsp; [SUBDIVISION]&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:50:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334539#M26095</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-11T15:50:52Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked Label Expression in Python</title>
      <link>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334540#M26096</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you for the reply.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I typed this into the Expression box with Python selected as the Parser and Advanced checked.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def FindLabel ( [LOT]&amp;nbsp; , [SUBDIVISION]&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; return "Name: " + [LOT]&amp;nbsp; + '\n' + [SUBDIVISION]&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I got an error,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Error 0 on line 0.&lt;/P&gt;&lt;P&gt;Error running expression:&lt;/P&gt;&lt;P&gt;FindLabel(ESRIExpressionArg0,ESRIExpressionArg1)&lt;/P&gt;&lt;P&gt;Tracedback (most recent call last):&lt;/P&gt;&lt;P&gt;&amp;nbsp; File"&amp;lt;expression&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; File"&amp;lt;string&amp;gt;", line 2, in FindLabel&lt;/P&gt;&lt;P&gt;TypeError: cannot concatenate 'str' and 'NoneType' objects.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does this error have to do with 'null' values? Or do I need to cast the variable or do a type conversion?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Jan 2015 21:14:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334540#M26096</guid>
      <dc:creator>BrynDunbar</dc:creator>
      <dc:date>2015-01-20T21:14:18Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked Label Expression in Python</title>
      <link>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334541#M26097</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I had a similar problem recently. I think one of the label fields must have a null value. If you only have to worry about null values in subdivision (and both fields are strings) this should work:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def FindLabel ( [LOT], [SUBDIVISION] &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if [SUBDIVISION]:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; label = "Name: " + [LOT] + "\n" + [SUBDIVISION]&lt;/P&gt;&lt;P&gt;&amp;nbsp; else:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; label = "Name: " + [LOT]&lt;/P&gt;&lt;P&gt;&amp;nbsp; return label&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Jan 2015 21:33:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334541#M26097</guid>
      <dc:creator>ChristianHinderman</dc:creator>
      <dc:date>2015-01-20T21:33:14Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked Label Expression in Python</title>
      <link>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334542#M26098</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It is the NULL values in the fields that make these python expressions error out.&amp;nbsp;&amp;nbsp; The answer above solves that problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have one that looks like this, where sometimes [ContractName] and [ContractYear] are null, so I nest in the if statements.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def FindLabel ([ContractName], [ContractYear]):&lt;/P&gt;&lt;P&gt;&amp;nbsp; if [ContractName]:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if [ContractYear]:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; label = [ContractName] + '\n' + [ContractYear]&lt;/P&gt;&lt;P&gt;&amp;nbsp; else:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; label = "No" + '\n' + "Contract"&lt;/P&gt;&lt;P&gt;&amp;nbsp; return label&lt;IMG alt="Stacked Python Labels.JPG" class="jive-image image-1" src="/legacyfs/online/53062_Stacked Python Labels.JPG" style="width: 620px; height: 451px;" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Jan 2015 23:21:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334542#M26098</guid>
      <dc:creator>deleted-user-rmQdIupSdOqe</dc:creator>
      <dc:date>2015-01-20T23:21:21Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked Label Expression in Python</title>
      <link>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334543#M26099</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The other option is to use the labeling method "Define classes of features and label each class differently" option, setting up a SQL expression first, to filter out data that has NULL values.&lt;/P&gt;&lt;P&gt;&lt;IMG alt="sql for null values.JPG" class="jive-image image-1" src="/legacyfs/online/53066_sql for null values.JPG" style="width: 620px; height: 563px;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then your python label expression for that label class (my label class is Default) is:&lt;/P&gt;&lt;P&gt;&lt;IMG alt="stacked label python example.JPG" class="jive-image image-1" src="/legacyfs/online/53067_stacked label python example.JPG" style="height: auto;" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Jan 2015 23:28:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334543#M26099</guid>
      <dc:creator>deleted-user-rmQdIupSdOqe</dc:creator>
      <dc:date>2015-01-20T23:28:49Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked Label Expression in Python</title>
      <link>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334544#M26100</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You could change the expression to:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def FindLabel([LOT] ,[SUBDIVISION]):
&amp;nbsp; return "{0}\n{1}".format([LOT], [SUBDIVISION])&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It suppresses the error. However, this will return None if there is no data:&lt;/P&gt;&lt;P&gt;&lt;IMG alt="result0.png" class="jive-image image-1" src="https://community.esri.com/legacyfs/online/52933_result0.png" style="height: auto;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The nice part of using the python expression is that you can enhance the result, replacing the None for a different text&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def FindLabel([LOT] , [SUBDIVISION]):
&amp;nbsp; return "{0}\n{1}".format([LOT] if [LOT] != None else "No LOT",&amp;nbsp; [SUBDIVISION] if [SUBDIVISION] != None else "No SUBDIVISION")&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;... will result in:&lt;/P&gt;&lt;P&gt;&lt;IMG alt="result.png" class="jive-image image-2" src="https://community.esri.com/legacyfs/online/53069_result.png" style="height: auto;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;... or adding some formatting to the text:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def FindLabel([LOT] , [SUBDIVISION]):
&amp;nbsp; return "{0}\n{1}".format([LOT] if [LOT] != None else "&amp;lt;CLR red='255'&amp;gt;No LOT&amp;lt;/CLR&amp;gt;",&amp;nbsp; [SUBDIVISION] if [SUBDIVISION] != None else "&amp;lt;CLR red='255'&amp;gt;No SUBDIVISION&amp;lt;/CLR&amp;gt;")&lt;/PRE&gt;&lt;P&gt;will give:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="result2.png" class="jive-image image-3" src="https://community.esri.com/legacyfs/online/53070_result2.png" style="height: auto;" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:50:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334544#M26100</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-11T15:50:55Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked Label Expression in Python</title>
      <link>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334545#M26101</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I wouldn't go for label classes in this case, since in the example above, with a simple line I am tackling 4 classes,,,&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 21 Jan 2015 02:08:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334545#M26101</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2015-01-21T02:08:42Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked Label Expression in Python</title>
      <link>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334546#M26102</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you very much! This is the first time that i have used GeoNet and I am very pleased with the responses that I have received; what a great resource it is; and the sense of community that it provides for GIS users. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I used your example and put this in the Label Expression box:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def FindLabel ([LOT], [SUBDIVISION]):&lt;/P&gt;&lt;P&gt;&amp;nbsp; if [LOT]:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if [SUBDIVISION]:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; label = [LOT] + '\n' + [SUBDIVISION]&lt;/P&gt;&lt;P&gt;&amp;nbsp; else:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; label = "&amp;nbsp;&amp;nbsp; "&lt;/P&gt;&lt;P&gt;&amp;nbsp; return label&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It shows what I want it to show and I understand the code. This nesting idea was what I needed. Thanks again!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jan 2015 22:33:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334546#M26102</guid>
      <dc:creator>BrynDunbar</dc:creator>
      <dc:date>2015-01-23T22:33:24Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked Label Expression in Python</title>
      <link>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334547#M26103</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jan 2015 22:37:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334547#M26103</guid>
      <dc:creator>BrynDunbar</dc:creator>
      <dc:date>2015-01-23T22:37:07Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked Label Expression in Python</title>
      <link>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334548#M26104</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Christian! Your response and this thread definitely helped me find the solution that I was looking for. This is what I have so far.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def FindLabel ([LOT], [SUBDIVISION]):&lt;/P&gt;&lt;P&gt;&amp;nbsp; if [LOT]:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if [SUBDIVISION]:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; label = [LOT] + '\n' + [SUBDIVISION]&lt;/P&gt;&lt;P&gt;&amp;nbsp; else:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; label = "&amp;nbsp;&amp;nbsp; "&lt;/P&gt;&lt;P&gt;&amp;nbsp; return label&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It labels Lots and Subdivisions, but when they are Null, you can't see the label because I made it "&amp;nbsp; " say nothing but a space. I am sure there are better ways to write the code, but this seems to be doing what I want for now.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you again.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jan 2015 22:47:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334548#M26104</guid>
      <dc:creator>BrynDunbar</dc:creator>
      <dc:date>2015-01-23T22:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked Label Expression in Python</title>
      <link>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334549#M26105</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def FindLabel (&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt; [LOT], [SUBDIVISION] &lt;/SPAN&gt;):
&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; return &lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;[LOT] +&amp;nbsp; "\r"&amp;nbsp; +&amp;nbsp; [SUBDIVISION]&lt;/SPAN&gt;
&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 'this had a null value'&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Another succinct way to write this is:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def FindLabel ( [&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;LOT], [SUBDIVISION]&lt;/SPAN&gt; ):
&amp;nbsp; if [LOT] and [SUBDIVISION]:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return [LOT] + '\r' + [SUBDIVISION]&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:50:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stacked-label-expression-in-python/m-p/334549#M26105</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-11T15:50:58Z</dc:date>
    </item>
  </channel>
</rss>

