Creating a counter

Ian Kelly ian.g.kelly at
Wed Oct 15 21:17:11 CEST 2014

On Wed, Oct 15, 2014 at 12:39 PM, Shiva
<shivaji_tn at> wrote:
> Hi,
> I am trying to search a string through files in a directory - however while
> Python script works on it and writes a log - I want to present the user with
> count of number of strings found. So it should increment for each string found.
> How do I implement it?
> If I use a print() within a if condition statement - and execute the script
> in terminal - for each find, the print() prints in new line instead of a
> constantly incrementing counter.

Before the loop, initialize the counter to 0:

    counter = 0

Inside the loop, increment the counter:

    counter = counter + 1  # can be shortened to: counter += 1

Alternatively, if you have all the strings you want to count in a
list, you can just check the length of the list:


More information about the Python-list mailing list