I am trying to concatenate 2 string variables Var1 and Var2 using python (converting some old ArcGIS VB-based scripts to ArcPy). I need to add a backslash between them.

arcpy.CalculateField_management(TargetDB, "myfield", '!Var1!' + '\\' + '!Var2!', "PYTHON")

I get an error message that says

"SyntaxError: unexpected character after line continuation character"

when the code block runs. I believe the cause of the error is that Var2 contains a forward slash character. It isn't file or folder path related, it's an ID field that acts as a primary key in a database (which is frustrating, but something I can't do anything about).

Given that I can't really modify the values of Var2 because of the database situation, does anyone have any recommendations of a way around this in Python 3 to work in ArcGIS Pro 2.6? The old VB code worked, so I am assuming this is a python specific issue.

  • Where are you executing this code, iIn python window? Var1 and Var2 are fieldnames or variables? – BERA Aug 6 '20 at 9:20
  • It's a script called from a custom toolbox that I run from the toolboxes menu inside Pro. Var1 and Var2 are fieldnames in the attribute table of TargetDB. – Arianne Ford Aug 6 '20 at 22:34
  • The values of Var2 are like R/89456 or R/56371 etc. – Arianne Ford Aug 6 '20 at 22:45

Field Calculator and python interpreter are different tools. The former post-process what you introduce as expression and should not be treated "strictly" as a python interpreter. The error message says once processed, Field Calculator tool treats your code's + "\\" + as a code line continuation character (i.e., , for more details have a look at https://stackoverflow.com/questions/53162/how-can-i-do-a-line-break-line-continuation-in-python). In fact, '!Var1!' + '\\' + '!Var2!' is converted to !Var1!\!Var2! (you can verify this by copying this to Field Calculator).

To correct this error you need to thread string expression more carefully in python interpreter. For example, '!Var1!' + ' + "\\\\" + ' + '!Var2!' will work. If you evaluate this, it turns into !Var1! + "\\" + !Var2! which just works since it is a valid Field Calculator expression. Personally I find python string formatting utilities better suited and safer to generate expression, e.g., r"{} + '\\' + {}".format('!Var1!', '!Var2!').


You can use !Var2!.replace('<problem char>', '<replacement string>')in your calculation to modify the value held in Var2 as needed.

  • I do not think this will solve the OP's problem. I think having for/backward slashes is not of importance. – fatih_dur Aug 6 '20 at 8:08

I could easily be wrong on this one and what you're trying be valid, but I've only ever seen examples for or personally used a calculate field expression that is a single string. As I understand it, the string provided here is passed in and then parsed, so it can contain strings itself as long as you use single quote for the outer and double for strings within it, or the reverse... or you could use triple quotes, look up python docs for more on that, or ask and I can probably find it and point you in the direction.

Try: arcpy.CalculateField_management(TargetDB, "myfield", "!Var1! + '\\' + !Var2!", "PYTHON")

There may still be something else in there causing issues, but I'd try that first.

  • Good catch - the original string would behave as though '!Var1!\\!Var2!' was passed to the field calculator, which isn't valid python. The best approach to troubleshoot this is to simply use the field calculator to get that string part right before you attempt to write it out in python. – Anna Forrest Aug 5 '20 at 21:47

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.