<?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: Truncating String in Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/truncating-string-in-python/m-p/415143#M32683</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks everyone, for the help. I used a combination of your suggestions and now that portion of my script works perfectly!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 13 Jun 2011 14:44:25 GMT</pubDate>
    <dc:creator>MikeMacRae</dc:creator>
    <dc:date>2011-06-13T14:44:25Z</dc:date>
    <item>
      <title>Truncating String in Python</title>
      <link>https://community.esri.com/t5/python-questions/truncating-string-in-python/m-p/415137#M32677</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have poured through the internet trying to find a way to truncate a string in Python and I am at a wall.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I want to truncate a string like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My_String_Needs_to_be_truncated_12May11_mm&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I want to truncate this portion "_12May11_mm" so that I have "My_String_Needs_to_be_truncated" remains.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for any help!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Jun 2011 18:44:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/truncating-string-in-python/m-p/415137#M32677</guid>
      <dc:creator>MikeMacRae</dc:creator>
      <dc:date>2011-06-10T18:44:16Z</dc:date>
    </item>
    <item>
      <title>Re: Truncating String in Python</title>
      <link>https://community.esri.com/t5/python-questions/truncating-string-in-python/m-p/415138#M32678</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;What do other strings of the same form look like? Do you need to truncate to a length or a number of fields?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;x = "My_String_Needs_to_be_truncated_12May11_mm"
truncated = x[:31]&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;or&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;x = "My_String_Needs_to_be_truncated_12May11_mm"
truncated = x.rsplit("_")[0]&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;etc. What do other samples of strings you need truncating look like?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:48:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/truncating-string-in-python/m-p/415138#M32678</guid>
      <dc:creator>JasonScheirer</dc:creator>
      <dc:date>2021-12-11T18:48:52Z</dc:date>
    </item>
    <item>
      <title>Re: Truncating String in Python</title>
      <link>https://community.esri.com/t5/python-questions/truncating-string-in-python/m-p/415139#M32679</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you want to remove a certain number of characters from the right, use rstrip(). Alternatively, you can return a list of "words" contained in a string separated by a given character using split(), then keep the ones you want. Your best bet is to Google "python string operations".&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Jun 2011 18:51:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/truncating-string-in-python/m-p/415139#M32679</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2011-06-10T18:51:49Z</dc:date>
    </item>
    <item>
      <title>Re: Truncating String in Python</title>
      <link>https://community.esri.com/t5/python-questions/truncating-string-in-python/m-p/415140#M32680</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hey thanks for the replies. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jason, all my strings look like that. They all end exactly like my example with a "_12May11_mm" The exact amount of characters in that portion never changes (11) because I only change the date and the "mm" are my initials. If another user will be using my profile, the initials may change, but it's always just 2 characters. The amount of characters in the leading portion (My_String_Needs_to_be_truncated) can and will change often, so I wanted to tackle this from the last portion because it is more consistent.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Basically, this is the the format of my mxd file names. When my program updates the mxd, I want to resave the file with the current date and the users initials. I have a line that can do the update, I am just having trouble seperating the file name and then grabbing that first portion into a variable or something. Below is my code for updating the file name. I would like to store the "My_String_Needs_to_be_truncated" into the variable "sitename"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"Path Name:"&amp;nbsp; + outloc + "\\" + sitename + "_"&amp;nbsp; + "_" + strftime("%y%b%d") + "_" + user&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Jun 2011 19:11:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/truncating-string-in-python/m-p/415140#M32680</guid>
      <dc:creator>MikeMacRae</dc:creator>
      <dc:date>2011-06-10T19:11:24Z</dc:date>
    </item>
    <item>
      <title>Re: Truncating String in Python</title>
      <link>https://community.esri.com/t5/python-questions/truncating-string-in-python/m-p/415141#M32681</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here's a couple more options&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# this option removes the 11 right characters (if you know it will always be 11)
x = 'My_String_Needs_to_be_truncated_12May11_mm'
print x[0:-11]

# this option builds the string after putting it into an array split at the "_" and removing the last two elements
y = x.split("_")
y.pop()
y.pop()
z = ""
i = 1
for word in y:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if i == 1:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; z = z + word
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; z = z + "_" + word
&amp;nbsp;&amp;nbsp;&amp;nbsp; i = 2

print z&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:48:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/truncating-string-in-python/m-p/415141#M32681</guid>
      <dc:creator>TerrySilveus</dc:creator>
      <dc:date>2021-12-11T18:48:54Z</dc:date>
    </item>
    <item>
      <title>Re: Truncating String in Python</title>
      <link>https://community.esri.com/t5/python-questions/truncating-string-in-python/m-p/415142#M32682</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ah, if it's always the same like that then you can do this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;tail_to_clip = "_12May11_mm"
x = "My_String_Needs_to_be_truncated_12May11_mm"
truncated = x.rsplit("_")[:-len(tail_to_clip)]&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And then just swap out tail_to_clip as needed. Though the option with rstrip is a bit more robust.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:48:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/truncating-string-in-python/m-p/415142#M32682</guid>
      <dc:creator>JasonScheirer</dc:creator>
      <dc:date>2021-12-11T18:48:57Z</dc:date>
    </item>
    <item>
      <title>Re: Truncating String in Python</title>
      <link>https://community.esri.com/t5/python-questions/truncating-string-in-python/m-p/415143#M32683</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks everyone, for the help. I used a combination of your suggestions and now that portion of my script works perfectly!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Jun 2011 14:44:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/truncating-string-in-python/m-p/415143#M32683</guid>
      <dc:creator>MikeMacRae</dc:creator>
      <dc:date>2011-06-13T14:44:25Z</dc:date>
    </item>
  </channel>
</rss>

