New to Python. For in loops curiosity

Ian Kelly ian.g.kelly at gmail.com
Tue May 13 23:49:04 EDT 2014


On Tue, May 13, 2014 at 9:38 PM, Leonardo Petry
<leonardo.petry.br at gmail.com> wrote:
> The loop below (at the bottom) runs each line of the file
>
> fin = open('wordplay.txt');
> user_input = raw_input('Enter some characters: ')
> count = 0
> for line in fin:
>     word = line.strip()
>     if(avoids(word, user_input)):
>         count += 1;
>
> This is just too convenient.
> Basically my question is: Why is python not treating the contents of wordplay.txt as one long string and looping each character?

Because the iterator for file-like objects iterates over lines, not characters.



More information about the Python-list mailing list