<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Iterate with leading zeros in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/iterate-with-leading-zeros/m-p/403127#M31731</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here is one way to do it:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for i in range(1, 600, 1):
&amp;nbsp;&amp;nbsp;&amp;nbsp; print ((6 -&amp;nbsp; len(str(i))) * "0") + str(i)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 18:22:32 GMT</pubDate>
    <dc:creator>ChrisFox3</dc:creator>
    <dc:date>2021-12-11T18:22:32Z</dc:date>
    <item>
      <title>Iterate with leading zeros</title>
      <link>https://community.esri.com/t5/python-questions/iterate-with-leading-zeros/m-p/403126#M31730</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Does anyone out there know how to iterate with leading zeros?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I make a Text field, for example: TEST... and populate the first row with 000000.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The next iteration I want is 000001, then 000002, up 000600.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I can program sequential numbers, but these slice off the leading zeros.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Essentially, I'm trying to make an ordered unique ID field. I then perform a split on 600 cells to return 600 shapefiles, however in the output geodatabase they are ordered 1, 10, 11, 12, 13, ... , 19, 2, 21 and so on, rather than 1, 2, 3, 4, 5, ... , 600.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Feb 2012 22:42:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterate-with-leading-zeros/m-p/403126#M31730</guid>
      <dc:creator>SamCoggins1</dc:creator>
      <dc:date>2012-02-14T22:42:30Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate with leading zeros</title>
      <link>https://community.esri.com/t5/python-questions/iterate-with-leading-zeros/m-p/403127#M31731</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here is one way to do it:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for i in range(1, 600, 1):
&amp;nbsp;&amp;nbsp;&amp;nbsp; print ((6 -&amp;nbsp; len(str(i))) * "0") + str(i)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:22:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterate-with-leading-zeros/m-p/403127#M31731</guid>
      <dc:creator>ChrisFox3</dc:creator>
      <dc:date>2021-12-11T18:22:32Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate with leading zeros</title>
      <link>https://community.esri.com/t5/python-questions/iterate-with-leading-zeros/m-p/403128#M31732</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Use the .zfill() method:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; test = "1"
&amp;gt;&amp;gt;&amp;gt; test.zfill(10)
'0000000001'
&amp;gt;&amp;gt;&amp;gt;&amp;nbsp; &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:22:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterate-with-leading-zeros/m-p/403128#M31732</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2021-12-11T18:22:35Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate with leading zeros</title>
      <link>https://community.esri.com/t5/python-questions/iterate-with-leading-zeros/m-p/403129#M31733</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Putting Chris's and csny490's answers together:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for i in range(600): #use range(1,601) to get 1 to 600
&amp;nbsp;&amp;nbsp;&amp;nbsp; str(i).zfill(6)&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Will return '000000' to '0000599' (600 strings)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:22:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterate-with-leading-zeros/m-p/403129#M31733</guid>
      <dc:creator>BruceNielsen</dc:creator>
      <dc:date>2021-12-11T18:22:38Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate with leading zeros</title>
      <link>https://community.esri.com/t5/python-questions/iterate-with-leading-zeros/m-p/403130#M31734</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have written code using the .zfill, my only problem being is that Python doesn't recognise 'fd' as a value of the cell. If I do 'print fd' I get: &amp;lt;geoprocessing describe field object object at 0x07DE6890&amp;gt;... so the script fails at that line. Anyone out there know what to write in place of the ???? given in the script below?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My code so far:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
from arcpy import env
import os

env.workspace = r'C:\Test\Test.gdb'

wspce = r'C:\Test\Test.gdb'
fc = 'G10'
fdName = "SBCID"
arcpy.AddField_management(wspce + os.sep + fc, fdName, "STRING")

fdList = arcpy.ListFields(wspce + os.sep + fc)

for fd in fdList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print fd.name, "&amp;gt;&amp;gt;", fd.type
&amp;nbsp;&amp;nbsp;&amp;nbsp; print fd
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(wspce + os.sep + fc, fdName, str(fd.????).zfill(3))
&amp;nbsp;&amp;nbsp;&amp;nbsp; print wspce
&amp;nbsp;&amp;nbsp;&amp;nbsp; print os.sep + fc
&amp;nbsp;&amp;nbsp;&amp;nbsp; print fdName
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:22:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterate-with-leading-zeros/m-p/403130#M31734</guid>
      <dc:creator>SamCoggins1</dc:creator>
      <dc:date>2021-12-11T18:22:40Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate with leading zeros</title>
      <link>https://community.esri.com/t5/python-questions/iterate-with-leading-zeros/m-p/403131#M31735</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;maybe try something like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;arcpy.CalculateField_management(wspce + os.sep + fc, fdName, '"str(!' + fd.name + '!).zfill(3)"', "PYTHON_9.3", "")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Feb 2012 18:46:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterate-with-leading-zeros/m-p/403131#M31735</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2012-02-15T18:46:29Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate with leading zeros</title>
      <link>https://community.esri.com/t5/python-questions/iterate-with-leading-zeros/m-p/403132#M31736</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ok, this works and does exactly what I want it to do... Returns a value of 001 to 600 in a field called SBCID. Thanks for your help.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
from arcpy import env
import os

env.workspace = r'C:\Test\Test.gdb'

wspce = r'C:\Test\Test.gdb'
fc = 'G10'
fdName = "SBCID"
arcpy.AddField_management(wspce + os.sep + fc, fdName, "STRING")

rows = arcpy.UpdateCursor(fc)

for i, row in enumerate(rows):
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.SBCID = str(i + 1).zfill(3)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:22:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterate-with-leading-zeros/m-p/403132#M31736</guid>
      <dc:creator>SamCoggins1</dc:creator>
      <dc:date>2021-12-11T18:22:43Z</dc:date>
    </item>
  </channel>
</rss>

