How to capitalize words using Python
If your letters and words are all uppercase or lowercase in a field, and you want to capitalize the first letter of each word…
…We have some straightforward Python code just for you.
…All you’ll need is to use the field calculator in ArcGIS (ArcMap) combined with a bit of Python.
This step-by-step guide will help you get started. And it works for ArcGIS Pro too.
Title Case Transformation in Python
We will simply take any field, and change the first character to a capital letter. The other letters in the word will be lowercase.
- SAN FRANCISCO will become San Francisco
- san francisco will become San Francisco
- saN fRaNciSCo will become San Francisco
….So the first letter will be capitalized using some Python code.
We use the .title() string operator in Python to accomplish our objective.
Step 1. Create a new text field called “Capitals”
This text field will be used to store the capitalized words. Make the text length property large enough to hold all of the text from the other field.
Now, there should be a new text field called capitals in your table:
Step 2. Use field calculator
Right-click the new field “Capitals”.
Select the ‘Field Calculator’ option.
Step 3. Enter Python code
First, select the Python radio button.
In the expression text box, double-click the field with the existing text field that you want to capitalize the first letter. Type .title() after the field and press OK.
Here’s an example of the field calculator code block:
If you see the result, it has a new field with capital letters.
If you’re getting an error, try the following:
- Run this Python code block by selecting the records first.
- Do not capitalize .title() with a capital ‘T’
Capital letter results with ArcMap and Python
Python has a simple solution to capitalize the first letter in a text field.
You can also convert strings to all lowercase in a text field using the .lower() property. The .upper() returns a copy of a string with all the case characters converted to uppercase.
From titles to lowercase to uppercase…. Use these handy Python string operators as huge time-savers for your big data sets.
If you want to learn more about powerful libraries for Python, then take a look at these 10 Python libraries for GIS and mapping. Also, don’t forget about our guide on Python Maximum and Minimum as well.