I have a script where I calculate a couple of variables, add a new field then need to do a field calculation that field. Once I get everything set up, it's the expression that I have a hard time figuring out. I am using ArcGIS 10.2

denominator = 0.14
arcpy.CalculateField_management("My_Table", "LQ", ( ( "!Foreign_Born_Population!" / "!Population!" ) / denominator ) , "PYTHON_9.3")

I am thrown an error stating it cannot parse the string but I thought by wrapping the field name in !! that it would recognize it as a field.

If I were to open the field calculator in the attribute table and type in a function using VB Script, the function would look like:

( [Foreign_Born_Population] / [Population] ) / 0.14

I also tried using the VB Script in the function but that did not work.

I have seen a couple different solutions including with and without using a cursor. Do I need to use a cursor?

How can I write this function?

  • Are both of your fields classified as number fields? If one of them is a string field, that can mess things up. What happens when you just run the 1st part of the calculation without the / 0.14? And then run the / 0.14 on the field you just calculated? Also, have you tried running it without the quotes around the fields?
    – Baltok
    Commented Aug 3, 2016 at 13:26

1 Answer 1


Remove the quotations around the field names and pass the entire expression as a string. It can also be helpful to break out the expression on its own line when troubleshooting.

denominator = 0.14
expression = '( ( !Foreign_Born_Population! / !Population! ) / {} )'.format(denominator)
arcpy.CalculateField_management("My_Table", "LQ", expression, "PYTHON_9.3")
  • Worked as expected on a test feature class, thank you so much! Question: Do you mind explaining why 1) the first part is entirely entered as a string and the denominator gets .format(), 2) what the { } do in the code, how does it know to reference to the denominator? Are there instances when multiple { } may be used? How would it know what to reference to?
    – giscard78
    Commented Aug 3, 2016 at 13:57
  • 3
    The expression parameter has to be a string so it is helpful to use the built-in format() method on the string to perform the inline substitution of denominator. You can have many substitutions. The {} notation serves as a placeholder and will fill them in the order you pass them in the parameters (e.g. '{}{}{}{}'.format('a', 'b', 'c', 'd') becomes abcd. More info here: docs.python.org/2/library/stdtypes.html#str.format
    – Evan
    Commented Aug 3, 2016 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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