Answer: Python Puzzler #392

The answer is B. 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 ‘n’ appears.

Trace table

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

lettermatches?
b
a
nyes
a
nyes
a

The code again

word = "banana"
print(word.count("n"))

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