<?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: Can a user input variable be used in an expression variable string for CalculateField_management ? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/can-a-user-input-variable-be-used-in-an-expression/m-p/1245705#M66415</link>
    <description>&lt;P&gt;I appreciate the reply. I'm not currently using ArcGIS Pro because I work for a local government and we haven't made the jump to it for editing purposes and a few other reasons, not my decision unfortunately. I'm sure the change is coming soon, but for the time being we're using ArcMap 10.8. Any chance to make the expression work for that. The string literals don't appear to be supported for ArcMap&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
    <pubDate>Thu, 05 Jan 2023 17:48:37 GMT</pubDate>
    <dc:creator>MPach</dc:creator>
    <dc:date>2023-01-05T17:48:37Z</dc:date>
    <item>
      <title>Can a user input variable be used in an expression variable string for CalculateField_management ?</title>
      <link>https://community.esri.com/t5/python-questions/can-a-user-input-variable-be-used-in-an-expression/m-p/1245653#M66409</link>
      <description>&lt;P&gt;First off, I know there's a better title I could have used so I apologize for the confusion.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a short script I've written that takes a some user entered information parcel data a field the calculation will be done for.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Import arcpy module
import arcpy

# Script arguments
Parcels = arcpy.GetParameterAsText(0)

Field2Calc_for = arcpy.GetParameterAsText(1) #field type derived from Parcels

lengthField = arcpy.GetParameterAsText(2)  #field type derived from Parcels

widthField = arcpy.GetParameterAsText(3)  #field type derived from Parcels

num = arcpy.GetParameterAsText(2) #Long Type 

# Local variables

expression = r'reclass' + str((r'!' + + widthField + r'!', r'!' + lengthField + r'!' , num))

codeblock = """ def reclass(lengthField,widthField,num):
                   if ((lengthField/num) &amp;gt;= (widthField)):
                       return 'True'
                   else:
                       return '' """

# Process: Calculate Field
arcpy.CalculateField_management(Parcels, Field2Calc_for, expression, "PYTHON", codeblock)
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm having a difficult time figuring out how to correctly set the string for the expression variable. What I'd like to do is take my user input&amp;nbsp;lengthField &amp;amp; widthField parameters and use variable substitution in the expression variable. I'm having a hard time figuring out if that's possible and what the syntax would look like. The examples for the the&amp;nbsp;CalculateField_management method have the field name hard coded into the script, but I'd like to try to avoid that if possible so the user can choose the field.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I appreciate any help and I'm pretty sure it's just something dumb that I'm overlooking b/c that's usually the case.&lt;/P&gt;&lt;P&gt;Thanks,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Mark&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jan 2023 17:35:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-a-user-input-variable-be-used-in-an-expression/m-p/1245653#M66409</guid>
      <dc:creator>MPach</dc:creator>
      <dc:date>2023-01-05T17:35:44Z</dc:date>
    </item>
    <item>
      <title>Re: Can a user input variable be used in an expression variable string for CalculateField_management ?</title>
      <link>https://community.esri.com/t5/python-questions/can-a-user-input-variable-be-used-in-an-expression/m-p/1245692#M66414</link>
      <description>&lt;P&gt;If you are using ArcGIS Pro, which you should be by now, then &lt;A href="https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings" target="_self"&gt;7.1.1. Formatted String Literals - Python 3.11.1 documentation&lt;/A&gt; are your friend.&amp;nbsp; Try:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;expression = f"reclass(!{widthField}!,!{lengthField}!,!{num}!)"&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 05 Jan 2023 17:29:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-a-user-input-variable-be-used-in-an-expression/m-p/1245692#M66414</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2023-01-05T17:29:51Z</dc:date>
    </item>
    <item>
      <title>Re: Can a user input variable be used in an expression variable string for CalculateField_management ?</title>
      <link>https://community.esri.com/t5/python-questions/can-a-user-input-variable-be-used-in-an-expression/m-p/1245705#M66415</link>
      <description>&lt;P&gt;I appreciate the reply. I'm not currently using ArcGIS Pro because I work for a local government and we haven't made the jump to it for editing purposes and a few other reasons, not my decision unfortunately. I'm sure the change is coming soon, but for the time being we're using ArcMap 10.8. Any chance to make the expression work for that. The string literals don't appear to be supported for ArcMap&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jan 2023 17:48:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-a-user-input-variable-be-used-in-an-expression/m-p/1245705#M66415</guid>
      <dc:creator>MPach</dc:creator>
      <dc:date>2023-01-05T17:48:37Z</dc:date>
    </item>
    <item>
      <title>Re: Can a user input variable be used in an expression variable string for CalculateField_management ?</title>
      <link>https://community.esri.com/t5/python-questions/can-a-user-input-variable-be-used-in-an-expression/m-p/1245754#M66416</link>
      <description>&lt;P&gt;Something like this maybe. Its an old(er) school way (Sorry, I haven't tested it).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;expression = "reclass(!%d!,!%d!,!%d!)" % (lengthField, widthField, num)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jan 2023 18:52:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-a-user-input-variable-be-used-in-an-expression/m-p/1245754#M66416</guid>
      <dc:creator>KimberlyGarbade</dc:creator>
      <dc:date>2023-01-05T18:52:44Z</dc:date>
    </item>
    <item>
      <title>Re: Can a user input variable be used in an expression variable string for CalculateField_management ?</title>
      <link>https://community.esri.com/t5/python-questions/can-a-user-input-variable-be-used-in-an-expression/m-p/1245755#M66417</link>
      <description>&lt;P&gt;Thanks Kim, I'll give that a shot.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jan 2023 18:56:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-a-user-input-variable-be-used-in-an-expression/m-p/1245755#M66417</guid>
      <dc:creator>MPach</dc:creator>
      <dc:date>2023-01-05T18:56:41Z</dc:date>
    </item>
    <item>
      <title>Re: Can a user input variable be used in an expression variable string for CalculateField_management ?</title>
      <link>https://community.esri.com/t5/python-questions/can-a-user-input-variable-be-used-in-an-expression/m-p/1245820#M66422</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;expression = 'reclass(!{0}!,!{1}!,!{2}!)'.format(lengthField, widthField, num)&lt;/LI-CODE&gt;&lt;P&gt;works with python 2.7&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jan 2023 20:23:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-a-user-input-variable-be-used-in-an-expression/m-p/1245820#M66422</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2023-01-05T20:23:12Z</dc:date>
    </item>
    <item>
      <title>Re: Can a user input variable be used in an expression variable string for CalculateField_management ?</title>
      <link>https://community.esri.com/t5/python-questions/can-a-user-input-variable-be-used-in-an-expression/m-p/1246599#M66478</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/15530"&gt;@RhettZufelt&lt;/a&gt;&amp;nbsp; this worked great. I just need to adjust the expression a bit because num was an int type.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;expression = 'reclass(!{0}!,!{1}!,{2})'.format(lengthField, widthField, num)&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 09 Jan 2023 20:26:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-a-user-input-variable-be-used-in-an-expression/m-p/1246599#M66478</guid>
      <dc:creator>MPach</dc:creator>
      <dc:date>2023-01-09T20:26:53Z</dc:date>
    </item>
    <item>
      <title>Re: Can a user input variable be used in an expression variable string for CalculateField_management ?</title>
      <link>https://community.esri.com/t5/python-questions/can-a-user-input-variable-be-used-in-an-expression/m-p/1246602#M66479</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/119500"&gt;@KimberlyGarbade&lt;/a&gt;&amp;nbsp;I think this probably works too Kim, but I think it would need to be something like this:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;"reclass(!%s!,!%s!,%d)" % (lengthField, widthField, num)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 20:34:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-a-user-input-variable-be-used-in-an-expression/m-p/1246602#M66479</guid>
      <dc:creator>MPach</dc:creator>
      <dc:date>2023-01-09T20:34:17Z</dc:date>
    </item>
  </channel>
</rss>

