I have a "Model_Version" field in my feature class that is being used for record keeping. I do not intend to ever do any numeric operations with it, so I am storing it as a string field. Right now my model version is 1.0, so it is being stored as "1.0".

When I used the Field Calculator manually in ArcMap, I can get my desired value by entering "1.0" or '1.0'.

But when I use ArcPy as below, the resulting string in the feature class is "1", without the decimal and trailing zero:

arcpy.CalculateField_management(myLayer, "Model_Version", 
                                "1.0", "PYTHON3")

I have tried the following and get the same result of "1" every time:

arcpy.CalculateField_management(myLayer, "Model_Version", 
                                '1.0', "PYTHON3")
arcpy.CalculateField_management(myLayer, "Model_Version", 
                                str("1.0"), "PYTHON3")
arcpy.CalculateField_management(myLayer, "Model_Version", 
                                1.0, "PYTHON3")

Why is it not writing the string as I have coded it?


The expression that you are passing to arcpy.CalculateField_management() is not directly a value as such (either a string or a number), but rather a snippet of Python code to evaluate in order to get a value. The outer quotation marks do not delineate a string value within that Python code, but are rather the start and end of the actual Python code.

So you need to include another set of quote marks within the Python code itself to delineate a string.

One way of doing this would be to include single quote (for the string) within the double quotes (for the python code):

arcpy.CalculateField_management(myLayer, "Model_Version", "'1.0'", "PYTHON3")

Alternatively, you could use double-quotes within single-quotes, or you could use escaped double-quotes within double-quotes, etc.

Without doing this, the value in the Python code 1.0 is equal to 1, not to the string "1.0".

  • That explains it. I had previously used the CalculateField_management function to pass integer values to a field, which could be done without enclosing them in quotes. So I did not grasp the importance of enclosing the python snippet. Thanks for clearing that up!
    – Jeff
    Jan 8 '20 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.