Program Console Vignaankosh.com
Execution Panel
Console is empty.
Count Occurrences of a Given Character is an interactive PYTHON dry run visualizer from the Strings programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.
This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.
# Python program to count occurrences of a given character
text = "success"
search_char = 's'
# Method 1: Using count() method
count1 = text.count(search_char)
# Method 2: Using loop
count2 = 0
for char in text:
if char == search_char:
count2 = count2 + 1
# Method 3: Using list comprehension
count3 = len([char for char in text if char == search_char])
# Method 4: Using sum() with generator
count4 = sum(1 for char in text if char == search_char)
print("String:", text)
print("Character to count:", search_char)
print("Count (count() method):", count1)
print("Count (loop):", count2)
print("Count (list comp):", count3)
print("Count (sum generator):", count4)