I'm still in my infancy learning Python (also using arcpy), and I've got a field calculator expression that I am having trouble formatting. Hoping someone can help... I am using ArcMap 10.5.

I am trying to have a calculation return a value of "1" if a specific word is found in another cell (text string). So for example... A cell named "BODY_PART" contains the following string "BACK ARM LEG", I would need the expression to return a value of "1" into a new/blank field (already created) based on looking for the word "ARM".... In my mind, the equation would look something like this: If "BODYPARTS" contains "ARM" print "1" else print "0"(wrong syntax, I know).

Here is how far I got...

**in the Pre-Logic Script Code:

def findWholeWord(leg) :
  if 'LEG' in !BODYPARTS! :
  else :

This is giving me a Syntax error on Line 2.

  • Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format. – Midavalo Oct 16 '17 at 17:12

You run the field calculator on the field you want to update, eg LEG and in your expression you specify the field you want to query with your function eg BODYPARTS.

I would also include an .upper() to force it to find all case variances like LEG, leg, Leg, etc.

Instead of print use return to have it write back to the field in your attribute table.

Pre-logic script code:

def findWholeWord(bodyparts):
  if 'LEG' in bodyparts.upper():
    return 1
    return 0


  • Thank you! Thank you! Thank you! I just tested this and it worked perfectly! – ccolbert Oct 16 '17 at 17:10

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.