DEV Community

Cover image for for loops (python3)
francnstein
francnstein

Posted on

for loops (python3)

The for loop is commonly used to repeat some code a certain number of times. This is done by combining for loops with range objects.

------------------------------

ex.

for i in range(5):
print("hello!")

------------------------------

  • Now if you wanted to check if the amount of a specific letter within a string.

ex2.

name = 'HEEEELLLLLOO THHERREE'
count = 0
for letter in name:
if letter == 'E':
count = count + 1
print(count)

7

Note: there are easier ways to count letters within a word through built-in tools such as 'HEEEELLLLLOO THHERREE'.count(‘E’)

                                         FRANCNSTEIN © | Created 𝗐𝗂𝗍𝗁 ❤

Top comments (0)