<?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: Insert a Zero after a specific number of character in a string in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65125#M5354</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Laura,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You were SO close! Instead of using &lt;/SPAN&gt;&lt;SPAN style="font-style: italic; text-decoration: underline;"&gt;new=&lt;/SPAN&gt;&lt;SPAN&gt; on each line you need to use &lt;/SPAN&gt;&lt;SPAN style="font-style: italic; text-decoration: underline;"&gt;new+=&lt;/SPAN&gt;&lt;SPAN&gt;, otherwise you are replacing the variable &lt;/SPAN&gt;&lt;SPAN style="font-style: italic; text-decoration: underline;"&gt;new&lt;/SPAN&gt;&lt;SPAN&gt; and starting from scratch on each line.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, I would take pieces of your string like this &lt;/SPAN&gt;&lt;SPAN style="font-style: italic; text-decoration: underline;"&gt;field[2:4]&lt;/SPAN&gt;&lt;SPAN&gt;. That will grab the second and third characters. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def AddPunc(field):
 new=field[0:2]
 new+="-"+field[2:4]
 new+="-"+field[4:6]
 new+="-"+field[6:9]
 new+="-"+field[9:12]
 new+="-"+field[12:15]
 return new&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Let me know how it goes. Good luck!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 22:30:31 GMT</pubDate>
    <dc:creator>JoshuaChisholm</dc:creator>
    <dc:date>2021-12-10T22:30:31Z</dc:date>
    <item>
      <title>Insert a Zero after a specific number of character in a string</title>
      <link>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65119#M5348</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a string field called [PROJECT_NO].&amp;nbsp; It has 7 characters in the string.&amp;nbsp; Example:&amp;nbsp; &lt;/SPAN&gt;&lt;STRONG&gt;02WG028&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;We are changing our project number standard to eight characters, so I need to "insert" a zero after the 4th character from the left in the project number.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]24004[/ATTACH]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have spent almost two whole days trying to figure this out on line with no luck.&amp;nbsp; My head is going to explode!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks in advance for any help.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;P.S. this layer is an SDE layer that I am editing using a version.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 May 2013 16:33:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65119#M5348</guid>
      <dc:creator>RobSmith</dc:creator>
      <dc:date>2013-05-03T16:33:33Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a Zero after a specific number of character in a string</title>
      <link>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65120#M5349</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Try this in the field calculator, be sure to set the parser to Python:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Pre-logic code block&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;def AddZero(field): &amp;nbsp; new = field[:4] + "0" + field[4:] &amp;nbsp; return new;&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And use this as the expression:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;AddZero( !your_fieldname! )&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]24010[/ATTACH]&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 May 2013 17:11:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65120#M5349</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2013-05-03T17:11:09Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a Zero after a specific number of character in a string</title>
      <link>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65121#M5350</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;That worked great and now I feel stupid.&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What if I wanted to throw in and if, then testing for the length of the PROJECT_NO string.&amp;nbsp; How would I do that.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Try this in the field calculator, be sure to set the parser to Python:&lt;BR /&gt;&lt;BR /&gt;Pre-logic code block&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def AddZero(field):
&amp;nbsp; new = field[:4] + "0" + field[4:]
&amp;nbsp; return new;
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;And use this as the expression:&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
AddZero( !your_fieldname! )
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;[ATTACH=CONFIG]24010[/ATTACH]&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:30:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65121#M5350</guid>
      <dc:creator>RobSmith</dc:creator>
      <dc:date>2021-12-10T22:30:22Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a Zero after a specific number of character in a string</title>
      <link>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65122#M5351</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You can add an if/then condition very easily with a length test.&amp;nbsp; What kind of conditions were you thinking of adding?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is an example of changing where to add the 0:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def AddZero(field):
&amp;nbsp; # add after 4th char if len == 7
&amp;nbsp; if len(field) == 7:
&amp;nbsp;&amp;nbsp;&amp;nbsp; new = field[:4] + "0" + field[4:]&amp;nbsp; # here is the "then" statement
&amp;nbsp; elif len(field) == 9:
&amp;nbsp;&amp;nbsp;&amp;nbsp; new = field[:5] + "0" + field[5:]&amp;nbsp; # inserts 0 @&amp;nbsp; 'xxxxx0xxxx' (after fifth position)
&amp;nbsp; return new;
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:30:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65122#M5351</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-10T22:30:25Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a Zero after a specific number of character in a string</title>
      <link>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65123#M5352</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This worked out great.&amp;nbsp; I ended up adding conditions for 6,5,4 &amp;amp; 3 character project numbers.&amp;nbsp; Also I had a condition to just return the field if the project number was greater than 7.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for all your help.&amp;nbsp; This made my day.&amp;nbsp; I can now relax this weekend. :cool:&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;You can add an if/then condition very easily with a length test.&amp;nbsp; What kind of conditions were you thinking of adding?&lt;BR /&gt;&lt;BR /&gt;Here is an example of changing where to add the 0:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def AddZero(field):
&amp;nbsp; # add after 4th char if len == 7
&amp;nbsp; if len(field) == 7:
&amp;nbsp;&amp;nbsp;&amp;nbsp; new = field[:4] + "0" + field[4:]&amp;nbsp; # here is the "then" statement
&amp;nbsp; elif len(field) == 9:
&amp;nbsp;&amp;nbsp;&amp;nbsp; new = field[:5] + "0" + field[5:]&amp;nbsp; # inserts 0 @&amp;nbsp; 'xxxxx0xxxx' (after fifth position)
&amp;nbsp; return new;
&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:30:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65123#M5352</guid>
      <dc:creator>RobSmith</dc:creator>
      <dc:date>2021-12-10T22:30:28Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a Zero after a specific number of character in a string</title>
      <link>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65124#M5353</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am trying to add in punctuation into one of my fields.&amp;nbsp; I saw the response above and tried it, but it only took the last line of code.&amp;nbsp; I also tried adding a "+" in between every one, but that didn't work either.&amp;nbsp; I don't have much of a coding background so I don't really know what else to do.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am attaching a screen shot of the code I wrote (that doesn't work).&amp;nbsp; I also have a screen shot of a sample area.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Original data- State_PIN&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;newpin- (I just did this manually, but I am trying to get a field calculator to add in the punctuation for me into the [formatedPIN] field)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for any help!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Feb 2014 17:02:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65124#M5353</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2014-02-12T17:02:41Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a Zero after a specific number of character in a string</title>
      <link>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65125#M5354</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Laura,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You were SO close! Instead of using &lt;/SPAN&gt;&lt;SPAN style="font-style: italic; text-decoration: underline;"&gt;new=&lt;/SPAN&gt;&lt;SPAN&gt; on each line you need to use &lt;/SPAN&gt;&lt;SPAN style="font-style: italic; text-decoration: underline;"&gt;new+=&lt;/SPAN&gt;&lt;SPAN&gt;, otherwise you are replacing the variable &lt;/SPAN&gt;&lt;SPAN style="font-style: italic; text-decoration: underline;"&gt;new&lt;/SPAN&gt;&lt;SPAN&gt; and starting from scratch on each line.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, I would take pieces of your string like this &lt;/SPAN&gt;&lt;SPAN style="font-style: italic; text-decoration: underline;"&gt;field[2:4]&lt;/SPAN&gt;&lt;SPAN&gt;. That will grab the second and third characters. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def AddPunc(field):
 new=field[0:2]
 new+="-"+field[2:4]
 new+="-"+field[4:6]
 new+="-"+field[6:9]
 new+="-"+field[9:12]
 new+="-"+field[12:15]
 return new&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Let me know how it goes. Good luck!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:30:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65125#M5354</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2021-12-10T22:30:31Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a Zero after a specific number of character in a string</title>
      <link>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65126#M5355</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Almost!!!&amp;nbsp; It is cutting out the last "-" I need though...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried adding another line:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;new+="-"+field[15:18]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But that doesn't work.&amp;nbsp; Any ideas?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you!!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Feb 2014 11:50:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65126#M5355</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2014-02-13T11:50:46Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a Zero after a specific number of character in a string</title>
      <link>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65127#M5356</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This worked for me:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;code block:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
from itertools import *

def formatPID(in_pid, delimiter='-', groups=()):
&amp;nbsp;&amp;nbsp;&amp;nbsp; # groups are how you want to break it up
&amp;nbsp;&amp;nbsp;&amp;nbsp; in_pid = str(in_pid)
&amp;nbsp;&amp;nbsp;&amp;nbsp; it = iter(in_pid)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return delimiter.join(''.join(islice(it, i)) for i in groups)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Expression:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
formatPID(!Your_PID_Field!, '-', (2,2,2,3,3,3))
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EDIT: sorry, I forgot one group of 2's&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 15:59:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65127#M5356</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-12T15:59:03Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a Zero after a specific number of character in a string</title>
      <link>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65128#M5357</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Helle again Laura,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I missed the "." in your screen shot. Try this out:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def AddPunc(field):
 new=field[0:2]
 new+="-"+field[2:4]
 new+="-"+field[4:6]
 new+="-"+field[6:9]
 new+="-"+field[9:12]
 new+="."+field[12:15]
 new+="-"+field[15:18]
 return new
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note if you change &lt;/SPAN&gt;&lt;SPAN style="font-style: italic; text-decoration: underline;"&gt;[15:18]&lt;/SPAN&gt;&lt;SPAN&gt; to &lt;/SPAN&gt;&lt;SPAN style="font-style: italic; text-decoration: underline;"&gt;[15:]&lt;/SPAN&gt;&lt;SPAN&gt;, the script will append any extra characters to the end of the new string.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you're still having problems, make sure you're new field ('formatedPIN') has a length of &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;at least&lt;/SPAN&gt;&lt;SPAN&gt; 24 to allow for the new punctuation characters.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Good luck!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:30:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65128#M5357</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2021-12-10T22:30:34Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a Zero after a specific number of character in a string</title>
      <link>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65129#M5358</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Got it to work, thank you soo much!!!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Feb 2014 13:10:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/insert-a-zero-after-a-specific-number-of-character/m-p/65129#M5358</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2014-02-13T13:10:40Z</dc:date>
    </item>
  </channel>
</rss>

