Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use regex for this. Recursion for trimming a string is really not a good idea:</p> <pre><code>import re def trim_string(string): return re.sub(r'^([^0-9]+)(.*?)([^0-9]+)$', r'\2', string) </code></pre> <p>To break it down, the regex (<code>r'^([^0-9]+)(.*?)([^0-9]+)$'</code>) is like so:</p> <ul> <li><code>^</code> matches the start of a string.</li> <li><code>([^0-9]+)</code> matches a group of consecutive non-digit characters.</li> <li><code>(.*?)</code> matches a group of stuff (non-greedy).</li> <li><code>([^0-9]+)</code> matches another group of consecutive non-digit characters.</li> <li><code>$</code> matches the end of the string.</li> </ul> <p>The replacement string, <code>r'\2'</code>, just says to replace the matched string with only the second group, which is the stuff between the two groups of non-digit characters.</p> <hr> <p>But if you're <em>really</em> sure you want to use your existing solution, you need to understand how recursion actually works. When you call <code>return foo</code>, the function returns <code>foo</code> as its output. If you don't call <code>return</code>, you return <code>None</code> automatically.</p> <p>That being said, you need to <code>return</code> in every case of the recursion process, not just at the end:</p> <pre><code>def String_Trim(Raw_String): if Raw_String[0].isdigit() == False: New_String = Raw_String[1:] return String_Trim(New_String) elif Raw_String[-1].isdigit() == False: New_String = Raw_String[:-1] return String_Trim(New_String) else: print Raw_String return Raw_String </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload