How do I iterate through a list of strings and print each item?
<p>I have a list of strings and print each of the strings in the list, meaning not <code>['word1','word2','word3']</code> but instead: <code>word1</code>, <code>word2</code>, <code>word3</code>.</p>

<p>I tried doing this:</p>

<pre><code>for i in list:

    print list[i]
</code></pre>

<p>but I get the message </p>

<blockquote>
  <p>"list indices must be integers, not str"</p>
</blockquote>

<p>I am really confused on how I should actually do this?</p>

<p>Firstly, don't name your variable <code>list</code>, since that is a python built-in reserved class. You'll save yourself confusion later. Let's call it <code>lst</code>, here. </p>

<p>Now, to your error. </p>

<blockquote>
  <p>"list indices must be integers, not str"</p>
</blockquote>

<p><code>lst[i]</code> is accessing an <em>index</em>, but it "must be an integer". However, <code>i</code> is a <code>str</code> (A Python string class). What is <code>i</code>, though? Well, it is the element <code>in lst</code> for the current iteration. </p>

<hr>

<p>You could "debug" your script by just printing <code>i</code>, see what it is. </p>

<p>If you were still confused (like you see it printing <code>1</code>), then you should print <code>type(i)</code>, it will say <code>&lt;type 'str'&gt;</code>, and printing <code>repr(i)</code> would see <code>'1'</code>, so you would print <code>int(i)</code> to cast the string <code>'1'</code> to an int <code>1</code></p>

python string list