<?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: Python string slicing fails to return the desired data. in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-string-slicing-fails-to-return-the-desired/m-p/1244803#M66382</link>
    <description>&lt;P&gt;Love sliding windows questions&lt;/P&gt;&lt;LI-CODE lang="python"&gt;z = np.array(list('ABCDCDC'))  # -- your base string
s = np.array(list('CDC'))  # -- sub string for query
# -- numpy magic
import numpy as np
sl_window = np.lib.stride_tricks.sliding_window_view(z, 3)  # -- windowing
whr_eq = (sl_window == s[None, :]).all(-1)  # -- find out where equal
position = np.nonzero(whr_eq)[0]  # -- at what location

sl_window
array([['A', 'B', 'C'],
       ['B', 'C', 'D'],
       ['C', 'D', 'C'],
       ['D', 'C', 'D'],
       ['C', 'D', 'C']], dtype='&amp;lt;U1')
position
array([2, 4], dtype=int64)

sl_window[position] 
array([['C', 'D', 'C'],
       ['C', 'D', 'C']], dtype='&amp;lt;U1')

len(position)
2&lt;/LI-CODE&gt;&lt;P&gt;recorded for posterity&lt;/P&gt;</description>
    <pubDate>Tue, 03 Jan 2023 08:08:59 GMT</pubDate>
    <dc:creator>DanPatterson</dc:creator>
    <dc:date>2023-01-03T08:08:59Z</dc:date>
    <item>
      <title>Python string slicing fails to return the desired data.</title>
      <link>https://community.esri.com/t5/python-questions/python-string-slicing-fails-to-return-the-desired/m-p/1244793#M66379</link>
      <description>&lt;P&gt;I am trying to search for the sum of occurances of a substring within a string:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;string = 'ABCDCDC'&lt;BR /&gt;sub_string = 'CDC'&lt;BR /&gt;for i in range(len(string)-len(sub_string)):&lt;BR /&gt;print(string[i:len(substring)]&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I am unsure why this is my output:&lt;/P&gt;&lt;P&gt;ABC&lt;BR /&gt;BC&lt;BR /&gt;C&lt;/P&gt;&lt;P&gt;Should'nt it be:&lt;/P&gt;&lt;P&gt;ABC&lt;BR /&gt;BCD&lt;BR /&gt;CDC&lt;BR /&gt;DCD&lt;BR /&gt;CDC&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jan 2023 05:29:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-string-slicing-fails-to-return-the-desired/m-p/1244793#M66379</guid>
      <dc:creator>BusinessNews</dc:creator>
      <dc:date>2023-01-03T05:29:11Z</dc:date>
    </item>
    <item>
      <title>Re: Python string slicing fails to return the desired data.</title>
      <link>https://community.esri.com/t5/python-questions/python-string-slicing-fails-to-return-the-desired/m-p/1244800#M66380</link>
      <description>&lt;P&gt;I think you mean&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;print&lt;/SPAN&gt;(string[i:i+&lt;SPAN&gt;len&lt;/SPAN&gt;(sub_string)])&lt;/PRE&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 03 Jan 2023 07:13:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-string-slicing-fails-to-return-the-desired/m-p/1244800#M66380</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2023-01-03T07:13:27Z</dc:date>
    </item>
    <item>
      <title>Re: Python string slicing fails to return the desired data.</title>
      <link>https://community.esri.com/t5/python-questions/python-string-slicing-fails-to-return-the-desired/m-p/1244802#M66381</link>
      <description>&lt;P&gt;In addition, if you want to count the number of overlapping sub_strings in your string, maybe this could work:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;string = 'ABCDCDC'
sub_string = 'CDC'

results = 0
sub_len = len(sub_string)
for i in range(len(string)):
    if string[i:i+sub_len] == sub_string:
        results += 1
print (results)&lt;/LI-CODE&gt;&lt;P&gt;source:&lt;/P&gt;&lt;P&gt;&lt;A href="https://stackoverflow.com/questions/8899905/count-number-of-occurrences-of-a-substring-in-a-string#comment50169124_8900059" target="_blank"&gt;https://stackoverflow.com/questions/8899905/count-number-of-occurrences-of-a-substring-in-a-string#comment50169124_8900059&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jan 2023 07:27:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-string-slicing-fails-to-return-the-desired/m-p/1244802#M66381</guid>
      <dc:creator>JohannesBierer</dc:creator>
      <dc:date>2023-01-03T07:27:31Z</dc:date>
    </item>
    <item>
      <title>Re: Python string slicing fails to return the desired data.</title>
      <link>https://community.esri.com/t5/python-questions/python-string-slicing-fails-to-return-the-desired/m-p/1244803#M66382</link>
      <description>&lt;P&gt;Love sliding windows questions&lt;/P&gt;&lt;LI-CODE lang="python"&gt;z = np.array(list('ABCDCDC'))  # -- your base string
s = np.array(list('CDC'))  # -- sub string for query
# -- numpy magic
import numpy as np
sl_window = np.lib.stride_tricks.sliding_window_view(z, 3)  # -- windowing
whr_eq = (sl_window == s[None, :]).all(-1)  # -- find out where equal
position = np.nonzero(whr_eq)[0]  # -- at what location

sl_window
array([['A', 'B', 'C'],
       ['B', 'C', 'D'],
       ['C', 'D', 'C'],
       ['D', 'C', 'D'],
       ['C', 'D', 'C']], dtype='&amp;lt;U1')
position
array([2, 4], dtype=int64)

sl_window[position] 
array([['C', 'D', 'C'],
       ['C', 'D', 'C']], dtype='&amp;lt;U1')

len(position)
2&lt;/LI-CODE&gt;&lt;P&gt;recorded for posterity&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jan 2023 08:08:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-string-slicing-fails-to-return-the-desired/m-p/1244803#M66382</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2023-01-03T08:08:59Z</dc:date>
    </item>
    <item>
      <title>Re: Python string slicing fails to return the desired data.</title>
      <link>https://community.esri.com/t5/python-questions/python-string-slicing-fails-to-return-the-desired/m-p/1244905#M66383</link>
      <description>&lt;P&gt;Your code is doing exactly what you are telling it to do. Replace the lengths with numbers for testing and you can see it:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for i in range(7-3): # equates to for i in 4: or in each iteration: 0, 1, 2, 3 in 4:
    print(string[i:3]) # -&amp;gt; is going to be: string([0:3]) string([1:3]) string([2:3]) ... etc&lt;/LI-CODE&gt;&lt;P&gt;You're not moving the ending position so it is always at position 3, or 'C'. With your loop:&lt;/P&gt;&lt;P&gt;ABC -&amp;gt; [0:3]&lt;BR /&gt;BC -&amp;gt; [1:3]&lt;BR /&gt;C -&amp;gt; [2:3]&lt;/P&gt;&lt;P&gt;There are other solutions as Dan posted, but to show the difference of your code and how adding the end position 'slides' the slice:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;pairs = {}
for start_index in range(len(string)): # for 1 in 7:
    end_index = start_index + 3 #&amp;lt;- move the end index
    if len(string[start_index:end_index]) == 3: # &amp;lt;- check if the length of start index and end index (+3) is more than 3.
        combo = string[start_index:end_index]
        print(combo)  # &amp;lt;- print it
        # added for counting the items
        pairs[combo] = pairs[combo] = 1 if not pairs.get(combo) else pairs[combo] + 1
    else:
        print(f'end of string: {string[start_index:end_index]}')

print (pairs)
print(f' {sub_string} count: {pairs[sub_string]}')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Gives you the results:&lt;/P&gt;&lt;P&gt;ABC&lt;BR /&gt;BCD&lt;BR /&gt;CDC&lt;BR /&gt;DCD&lt;BR /&gt;CDC&lt;/P&gt;&lt;P&gt;end of string: DC&lt;BR /&gt;end of string: C&lt;/P&gt;&lt;P&gt;{'ABC': 1, 'BCD': 1, 'CDC': 2, 'DCD': 1}&lt;BR /&gt;CDC count: 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jan 2023 15:16:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-string-slicing-fails-to-return-the-desired/m-p/1244905#M66383</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-01-03T15:16:00Z</dc:date>
    </item>
  </channel>
</rss>

