# Field Calculator expression question!

584
13
07-28-2011 08:17 AM
New Contributor
I have a field that has City, State, Zip in it. I'd like a new field with the zip code only in it, and then I'd like to be able to remove the zip from the original field. Is there a way to do this?

Possibly an expression that removes all #'s?
Tags (3)
13 Replies
Regular Contributor
First, read this blog post about concatenating fields using the Calculate Field tool.  It explains how to create a Python routine for Calculate Field to run to fetch a new field value.

Assuming you're dealing with 5-digit zip U.S. zip codes, and that the zip code falls at the end of the address, here's a snippet that shows one way you can parse it.

```>>> address = "380 New York St Redlands Ca 92373  "  # Note: trailing blanks
380 New York St Redlands Ca 92373

33

>>> print zip
92373

380 New York St Redlands Ca
```
Regular Contributor
Another alternative would be to use zip = address.strip()[-1]. That will return the last set of non-whitespace characters in the address string, regardless of length. You could then use slicing with rfind to get the first portion of the string:

```>>> address = "380 New York St Redlands Ca 92373  "
>>> zip
'92373'
'380 New York St Redlands Ca '```
New Contributor
Thanks for both of your replies! I failed to mention in my haste that some of the entries have not only the 5 digit US ZIP code but have the 4 digit ZIP tagged on at the end as well, for example:

"Town, NY 12345-1234"

Bruce, by last set of non-whitespace characters characters, what do you mean? I'm utterly inexperienced at coding/programming, pardon the questions.

Thanks for the help!
Regular Contributor
Whitespaces are spaces, tabs, etc. When using the split method without any parameters, it also removes those extra characters from the end of the string. Technically, the split method creates a Python list of items from a string based on a delimiter character in the string. The default is a space. Since you only want the last item in that list (the zip code), the [-1] is added to tell Python to only return the last item in the list that split created.

If you want to learn more, start with the tutorial that is included with Python's help. IMHO it's a good place to get familiar with what Python can do as far as string manipulation.
New Contributor
That would work perfectly! Do I need to replace where you have "address" with the name of the field that has the address in it, or am I NOT doing this calculation in a new blank field. I'm thinking that this is a calculation done within the same field.

Do I include the >>>'s?

I will certainly check out the tutorial for Python. Is that located within ArcGIS or separately?

```>>> address = "380 New York St Redlands Ca 92373  "
>>> zip
'92373'
'380 New York St Redlands Ca '```

Thanks
Regular Contributor
I don't normally use Python within the Calculate Field tool, but I think you would use an expression like !fieldname!.split()[-1], where fieldname is the column with your existing city/state/zip string. The blog post that Dale referenced should provide enough details to get you started.

The '>>>' are prompts from Python's interactive interpreter. Don't include those in your code.

The Python Help is found under Python 2.x as Python Manuals if you're using ArcGIS 9.x. For ArcGIS 10, I think you go to ArcGIS, then Python 2.x, then look for the help docs.
Regular Contributor
Good one, Bruce.

```>>> address = "380 New York St Redlands Ca 92373  "
`!fieldname!.split()[-1]`