title: Get string between the last two slash positions in a URL in Python
body: <p>I am working to slice an HTML address to get the unique part and use it as my filename.</p>
  <p>Here is the challenge:</p>
  <ul>
  <li><a href="http://www.someurl.com/folder-1/somestring/01.pdf" rel="nofollow noreferrer">http://www.someurl.com/folder-1/somestring/01.pdf</a></li>
  <li><a href="http://www.someurl.com/folders1531as12/anotherstring/183.pdf" rel="nofollow noreferrer">http://www.someurl.com/folders1531as12/anotherstring/183.pdf</a></li>
  <li><a href="http://www.someurl.com/folder-dsa990s/nostring/46798.pdf" rel="nofollow noreferrer">http://www.someurl.com/folder-dsa990s/nostring/46798.pdf</a></li>
  </ul>
  <p>I am trying to set the filename as:</p>
  <pre>
  somestring-01.pdf  
  anotherstring-01.pdf  
  nostring-01.pdf  
  </pre>
  <p>Since there is no way of knowing how many characters after the last slash and between the last two slashes, I am not able to hard-define splits such as [-5:-10].</p>
  <p>To be able to solve this challenge, my pseudo-code to get the filename is as follows:</p>
  <ul>
  <li>Find the index of the last string [int_last_slash_index]</li>
  <li>Find the index of the one previous string [int_prev_slash_index]<br />
  Step 1: count no of slashes in string<br />
  Step 2: subtract one from the count (count_slash-1)<br />
  Step 3: find the (count_slash-1)th index position</li>
  <li>Set slicing positions:<br />
  Position 1: last slash position = len(url) - int_last_slash_index<br />
  Position_2: previous slash position = len(url) - int_prev_slash_index</li>
  <li>Slice the URL string with [-int_prev_slash_position:-int_last_slash_position]</li>
  </ul>
  <p>In Python:</p>
  <pre><code>last_slash_index = url_string.rfind(&quot;/&quot;)
  int_last_slash_index = int(last_slash_index)
  int_last_slash_position = len(url_string) - int(last_slash_index)
  slash_count = url_string.count(&quot;/&quot;)
  one_prev_slash = slash_count -1 
  index_one_prev_slash = url_string.find(&quot;/&quot;,one_prev_slash)
  int_one_prev_slash_index = int(index_one_prev_slash)
  int_one_prev_slash_position = len(url_string) - 
  int(int_one_prev_slash_index)
  filename = url_string[-int_last_slash_position:-int_one_prev_slash_position]
  </code></pre>
  <p>If there is such a way, I want to solve it with string operators, rather than diving into regexes, code tricks because I cannot handle them now. I am OK to learn further methods, libraries though.</p>
  <p>As you would guess, I am new in Python and just trying to get a hold of strings.</p>
  <p>Thank you.</p>
  <p>PS: Just the opposite was posted before but for Java, no responses: <a href="https://stackoverflow.com/questions/29785732/remove-the-string-between-the-last-two-slashes-in-a-url">1</a></p>