Kodeclik Logo

Our Programs

Courses

Gifting

Learn More

Schedule

Kodeclik Blog

How to count in a Python loop

Python is such an expressive language that you might have to un-learn a lot of the old style programming habits you might have picked up from other languages.

For instance, the old style way of looping through the characters in a string looks like:

mystring = "Kodeclik Online Academy"
for i in range(0,len(mystring)):
  print(mystring[i],end='')
print()

The output will be:

Kodeclik Online Academy

In this program we iterate through the characters of the string knowing that the first index is 0 and the last index is one less than the length of the string. The for loop provides an index (the variable i) which we use inside the loop to fetch the specific character.

Of course if you are a seasoned Python programmer you know that this is not the Python-ic way of doing things. The more elegant solution is:

mystring = "Kodeclik Online Academy"
for i in mystring:
  print(i,end='')
print()

with the same output as before.

Here the for loop iterates over the string and because strings are iterables in Python each iteration of the for loop fetches one character at a time. The variable “i” here refers to the actual characters in the string “mystring”, not the indices.

What if you would like both? I.e., you wish to iterate over the characters and also need a counter for some purposes? Python provides an elegant way to do that using the enumerate function.

The enumerate function takes as input an iterable (such as a string in our example) and returns two values, one the index and the other the actual value from the iterable.

Python count in a for loop

So you can replace the above program by:

mystring = "Kodeclik Online Academy"
for (a,b) in enumerate(mystring):
  print(b,end='')
print()

The output will still be:

Kodeclik Online Academy

Note that enumerate returns two variables (a and b) but we are not really using the first variable for anything. Here’s how you can use it to count in a loop:

mystring = "Kodeclik Online Academy"
for (a,b) in enumerate(mystring):
  print("Character no: " + str(a+1) + " is " + b)
print()

The output will be:

Character no: 1 is K
Character no: 2 is o
Character no: 3 is d
Character no: 4 is e
Character no: 5 is c
Character no: 6 is l
Character no: 7 is i
Character no: 8 is k
Character no: 9 is  
Character no: 10 is O
Character no: 11 is n
Character no: 12 is l
Character no: 13 is i
Character no: 14 is n
Character no: 15 is e
Character no: 16 is  
Character no: 17 is A
Character no: 18 is c
Character no: 19 is a
Character no: 20 is d
Character no: 21 is e
Character no: 22 is m
Character no: 23 is y

Note that we are using the str() function to convert the index returned by enumerate (which will be an integer) into a string so that we can use it for concatenation. Also note that indices returned by enumerate() begin at 0, so we are adding 1 for ease of understanding.

Thus, enumerate() is a very useful function for counting inside a loop.

Interested in more things Python? See our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!

Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.