# Setting up Concatenation using Python expression in field calculator?

I am having trouble creating a Python expression in Field Calculator in ArcMap 10.3.1. I’m attempting to concatenate two field values (double and short integer) to a third (double) field, (CON_IN). I need to allow only two digits to be added to the system number, so if the ET_NO value is greater than or equal to 10, do not add “0”. If the ET_NO is less than or equal to 9, add “0” and the value.

The final output should look like this: 2161400187509 or 2161400187528

I’ve tried variations of this script but get basically the same results. The resulting error shows a syntax error and that SYS_NO is not defined.

Pre-logic Script Code:

``````def   Concat(x,y):

x= SYS_NO

y= IN_ETNO

if y >= 10:

return x +  y

elif y<= 9:

return x +  "0" +  y
``````

Expression:

``````CON_IN =  Concat( !SYS_NO! , !IN_ETNO! )
``````

I see I didn't need to define the x,y. I see by the other posts that I am trying to concatenate a number and a string value, but when I remove the quotation marks on the string it just sums the SYS_NO and ET_NO values which is not what I need. I am not adding the values together, I am trying to add a two digit code to the end of a system number based on unique values for each record.

• Times by 100, add number? – phloem Apr 1 '16 at 18:30
• You've already set the value of x and y when you called the function, so no need to try to set them within the function. – phloem Apr 1 '16 at 18:34
• don't remove the quote marks. Passing a numeric string value to a numeric field will store the number – Midavalo Apr 1 '16 at 19:07

If you start with a double and want to end with a double, why bother switching to string and back again? Move your SYS_NO over two places by multiplying by 100, then add the IN_ETNO value.

``````(!SYS_NO! * 100) + !IN_ETNO!
``````

Note that if you're using shapefiles, you may not be able to store the final digit, while GDB feature classes will.

• this works for me – Midavalo Apr 1 '16 at 19:27

Your error is likely due to the fact that you're trying to concatenate numeric values with a string. You'll need to convert the other values before trying to concatenate with a '0'. Having said that, it seems like you're simply trying to pad the 'y' value with a single '0'. If this is correct, then you can actually use the zfill function to pad the final digit up to a total length of 2.

```#if x value is a double, you'll need to remove the decimal def concat(x,y): return str(x).replace('.','') + str(y).zfill(2)```

This can be simplified quite a bit with `string.format()`, which can be configured to zero-pad your values. You don't need a code block at all, actually:

``````"{f1}{f2:02d}".format(f1=!CON_IN!,f2=!ET_NO!)

>>> "{f1}{f2:02d}".format(f1=21614001875,f2=9)
'2161400187509'
>>> "{f1}{f2:02d}".format(f1=21614001875,f2=28)
'2161400187528'
``````

The reason you're getting the message `SYS_NO is not defined` is because you have already set `x = !SYS_NO!` by passing `CON_IN = Concat( !SYS_NO! , !IN_ETNO! )`.
In `def Concat(x,y):` you've specified that the first parameter is `x` and the second is `y`, so passing `!SYS_NO!` as your first parameter means that you're setting `x = !SYS_NO!` there.

``````def   Concat(x,y):
xx = int(x)
yy = int(y)
if yy >= 10:
return '{}{}'.format(xx, yy)
elif yy<= 9:
return '{}0{}'.format(xx, yy)
``````

Expression:

``````CON_IN =  Concat( !SYS_NO! , !IN_ETNO! )
``````
• Thanks, Midavalo. I tried this and it just returns the original system number in the CON_IN field without the two digit code value from the IN_ETNO field. – SGanger Apr 1 '16 at 19:12
• you are right. @phloem's answer should work though. Be sure you have no decimals in your double SYS_NO field – Midavalo Apr 1 '16 at 19:28
• Your example looks like it should work but it doesn't add the IN_ETNO code value to the system number in the output field. Is there something else that I need to do. I have the SYS_NO field properties set to 0 decimal places, is that ok? – SGanger Apr 1 '16 at 19:30
• convert your numbers to `int` first and it will work. I have edited my answer to show this – Midavalo Apr 1 '16 at 19:42
• I'm not sure if changing the format to integer will cause problems with my report processing program. It is in Access and the macros are temperamental. My database has about twenty thousand records that need to be calculated. I'll have to check that out before I proceed with this. Thanks for your help. – SGanger Apr 1 '16 at 19:51