Meaning Use of Curly Brackets in ArcPy?

3121
10
Jump to solution
06-26-2017 01:45 PM
NealBanerjee
Occasional Contributor

Hello - in a number of P Stand Alone Python examples associated with a number of geoprocessing tools (e.g. Add Feature Class to Terrain), there is some syntax that uses curly brackets that I dont understand and dont see documentation.  In general Python I understand curly brackets are used to define "dictionaries", however use in geoprocessing tools with ArcPy seems different.  Can anyone explain their use or point me to documentation?

Below is example from a tool example that uses curly brackets

Thanks

Neal

Add Feature Class To Terrain (3D Analyst)

# Create the file gdb that will store the feature dataset    
arcpy.management.CreateFileGDB(gdbLocation, gdbName)    
gdb = '{0}/{1}'.format(gdbLocation, gdbName)    
# Obtain spatial reference from TIN    
SR = arcpy.Describe(tin).spatialReference    
# Create the feature dataset that will store the terrain    
arcpy.management.CreateFeatureDataset(gdb, fdName, SR)    
fd = '{0}/{1}'.format(gdb, fdName)    
(...)
0 Kudos
10 Replies
curtvprice
MVP Esteemed Contributor

This thread doesn't mention that besides string messages, this formatting is very handy for creating SQL expressions, calculate field expressions, etc.

>>> "VALUE = {} OR VALUE = {}".format(1,2) # numeric field
'VALUE = 1 OR VALUE = 2'
>>> "VALUE = '{}' OR VALUE= '{}'".format(1,2) # text field
"VALUE = '1' OR VALUE = '2'"
>>> "VALUE IN ({})".format(
...     ",".join(["'{}'".format(k) for k in [0,1,2]])) # list
"VALUE IN ('0','1','2')"
>>> "({:.3f} - !{}!) / 100".format(25, "FIELD") # Calculate Field expression
'(25.000 * !FIELD!) / 100'
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

You can do a lot more with string formatting to make pretty strings with an entire formatting mini-language.

Nice to know for labeling maps with Python expressions!

Python String Format Cookbook – mkaz.tech