Answer: Python Puzzler #382

The answer is D. The program prints:

2
What the program prints
Checked by running the program for real.

Why?

count goes through the string and counts how many times ‘b’ appears.

Trace table

A trace table follows the program step by step. Here’s this one:

lettermatches?
a
byes
r
a
c
a
d
a
byes
r
a

The code again

word = "abracadabra"
print(word.count("b"))

Try it: change one number in the program and predict the new output before you run it.