How do I apply Title Case to just a portion of a text field

1000
4
Jump to solution
03-14-2013 02:29 PM
BobPerham
Occasional Contributor
I have an address field in all caps and I want to convert just the city to title case.  So I currently have:

AKRON, OH 12345
BREA, CA 54321

And I want to see:

Akron, OH 12345
Brea, CA 54321

Basically I want to know how to convert just a portion of a string, in this case the portion before the comma.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LucasDanzinger
Esri Frequent Contributor
You can integrate either of these into the field calculator. Select to use Python for the parser, and check to Show Codeblock.

Expression should be:
title(!fieldName!)


Code Block:
def title(name):   alist = name.split(",")   city = alist[0].title()   final = city + "," + alist[1]   return final

View solution in original post

0 Kudos
4 Replies
LucasDanzinger
Esri Frequent Contributor
There could be a more elegant way, but this should do the trick:

>>> a = 'AKRON, OH 12345'
>>> alist = a.split(",")
>>> city = alist[0].title()
>>> final = city + "," + alist[1]
>>> print final
Akron, OH 12345
0 Kudos
MathewCoyle
Frequent Contributor
Lucas has it right I'd say, I'd maybe do something like this. Essentially the same thing.

line = 'AKRON, OH 12345'
line_split = line.split(',')
final_line = '{0},{1}'.format(line_split[0].title(), line_split[1])
print final_line
0 Kudos
BobPerham
Occasional Contributor
I'm sorry.  Maybe I should have mentioned, I need to do this in the field calculator.  The field name is CITYSTATE.  So normally I would just enter !CITYSTATE!.title() and it would Title Case the whole value.  Do I need to check off "Show Codeblock" and add it as pre-script logic?
0 Kudos
LucasDanzinger
Esri Frequent Contributor
You can integrate either of these into the field calculator. Select to use Python for the parser, and check to Show Codeblock.

Expression should be:
title(!fieldName!)


Code Block:
def title(name):   alist = name.split(",")   city = alist[0].title()   final = city + "," + alist[1]   return final
0 Kudos