Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!news.linkpendium.com!news.linkpendium.com!panix!gordon From: John Gordon Newsgroups: comp.lang.python Subject: Re: New to Python. For in loops curiosity Date: Wed, 14 May 2014 03:46:30 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 27 Message-ID: References: <2f08e970-1334-4e7f-ba84-14869708a73b@googlegroups.com> NNTP-Posting-Host: panix2.panix.com X-Trace: reader1.panix.com 1400039190 2522 166.84.1.2 (14 May 2014 03:46:30 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Wed, 14 May 2014 03:46:30 +0000 (UTC) User-Agent: nn/6.7.3 Xref: csiph.com comp.lang.python:71527 In <2f08e970-1334-4e7f-ba84-14869708a73b@googlegroups.com> Leonardo Petry writes: > 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? > Any comment is greatly appreciate. Thanks Your code is not treating the contents of wordplay.txt as one long string because 'for line in fin:' tells it to read line-by-line. If you want to read the entire contents, use the read() method: file_content = fin.read() -- John Gordon Imagine what it must be like for a real medical doctor to gordon@panix.com watch 'House', or a real serial killer to watch 'Dexter'.