3

I am using ArcMap 10.2.2.

I have three attribute fields (Address_No: 1, StreetName: Main, Street_Ty: ST) that I want to merge together. How do I concatenate string data found within the table plus numeric data?

I would like the following result: 1 attribute field - Address: 1 Main ST

7

No need to write a function here. Just hit the Python radio button, and then write this into the box:

 Address: + " " + str(!Address_No!) + " " + !StreetName! + " " + !Street_Ty!

What is important:
- field names are represented by their name but embraced by exclamation points: !
- any numeric field needs to be converted to a string when concatenating with other strings. To do that in Python, use the str() function. Anything within the parentheses will be converted o a string, even if it is a number
- You should add blank characters (" ") between your parts, to format this nicely, so it appears like: Address: 1 Main ST instead of Address:1MainST

2

I would use a simple Python script within the field calculator tool.

Add this in the Codeblock box:

def my_concatenation(address_nr, my_street_name, my_street_ty):
    my_address_nr = str(address_nr)    # converts the address number into strings so that it can concatenated with the rest.
    my_result_string = "Address: {0} {1} {2}".format(my_address_nr,my_street_name,my_street_ty)  # adds "Address:", concatenates all the strings with space in between.
    return my_result_string

Then in your expression field (below codeblock), you would type:

my_concatenation(!Address_No!, !StreetName!, !Street_Ty!)

I am assuming he attribute field names you use in your question are the actual names of the fields. If not replace those names between the "!" in your expression. Make sure to check the Python parser radio button in your calculate field window.

  • I get <Null> as a value – Snickers Mar 15 '15 at 1:58
  • Think there's also an error in forming the result string, missing a '+'. I recommend using format function. – T. Wayne Whitley Mar 15 '15 at 2:02
  • Not sure what this all means (I am new at Arcmap) – Snickers Mar 15 '15 at 2:10
  • @Jelle, about the string formatting, this is perhaps a more conducive way to enter string variables, see examples here: docs.python.org/2/library/string.html?#format-examples. I think if you provided a screenshot for Snickers, it would be best. (I'm not logged in; maybe you are?) – T. Wayne Whitley Mar 15 '15 at 2:10
  • 1
    could be a simple error in formatting the string...recommend replacing the my_result_string line with: my_result_string = "Address: {0} {1} {2}".format(my_address_nr,street_name,my_street_ty) – T. Wayne Whitley Mar 15 '15 at 2:24
1

The VB Script version/alternative to BritishSteel's answer:

Add a new text field with enough characters to hold your longest address to the attribute table (must be done outside of an edit session). Right-click on the new field and choose Field Calculator (in or out of an edit session, but out of means no 'undo'). In the Fields box, double click the first field you want to include. It's added to the lower box. To create a space between it and the value of the next field you add, type

& " " &

in the lower box after the first field name. I like to leave spaces in the expression to make it readable, but remember only spaces inside quotes will be rendered into the output. If you didn't want spaces, you'd just use & (ampersand) which is the concatenating character/symbol like + is in Python. Also note in Python field names are enclosed by exlamations, but in VB Script it's brackets. Double-click the next field to add it to the expression, add the spacing and ampersand characters, and so on until you have all desired fields.

enter image description here

1

You can use Python string formatting in the Field Calculator without using a code block, which makes things a bit cleaner for simple operations:

Field Calculator String Formatting

As a general rule it's usually better to use string formatting and avoid using concatenation. This is especially true when you want to have more complex numeric formatting, such as currency, or date formatting. I would also suggest you take a look at the Python docs to see more examples of how you can use string formatting:

Python String Formatting

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.