Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="http://docs.python.org/library/string.html" rel="noreferrer">string.join</a> doesn't do what you think. join is used to combine a list of words into a single string with the given seperator. Ie:</p> <pre><code>&gt;&gt;&gt; ",".join(('foo', 'bar', 'baz')) 'foo,bar,baz' </code></pre> <p>The code snippet you posted will attempt to insert myString between every character in the variable line. You can see how that will get big quickly :-). Are you trying to read the entire file into a single string, myString? If so, the way you want to concatenate the strings is like this:</p> <pre><code>myString = myString + line </code></pre> <p>While I'm here... since you're learning Python here are some other suggestions.</p> <p>There are easier ways to read an entire file into a variable. For instance:</p> <pre><code>fileIN = open("testdata.txt", "r") myString = fileIN.read() </code></pre> <p>(This won't have the exact behaviour of your existing strip() code, but may in fact do what you want.)</p> <p>Also, I would never recommend practical Python code use recursion to iterate over a string. Your code will make a function call (and a stack entry) for every character in the string. Also I'm not sure Python will be very smart about all the uses of bigString[1:]: it may well create a second string in memory that's a copy of the original without the first character. The simplest way to process every character in a string is:</p> <pre><code>for mychar in bigString: ... do your stuff ... </code></pre> <p>Finally, you are using the list named "checked" to see if you've ever seen a particular character before. But the membership test on lists ("if myChar in checked") is slow. In Python you're better off using a dictionary:</p> <pre><code>checked = {} ... if not checked.has_key(myChar): checked[myChar] = True ... </code></pre> <p>This exercise you're doing is a great way to learn several Python idioms.</p>
 

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