Count the Number of Occurrences of a Character in a String in Python

Python Program to Count the Number of Occurrences of a Character in a String Using Loops

# Count the Number of Occurrences of a Character in a String in Python using for loop

count = 0
my_string = "hello"
my_char = "l"
for i in my_string:
    if i == my_char:
        count += 1
print(count)

Output:

2

About The Author

Leave a Reply