Python FizzBuzz Program

Short answer
FizzBuzz asks you to print the numbers 1 to 100, replacing multiples of 3 with Fizz, multiples of 5 with Buzz and multiples of both with FizzBuzz; check the combined case first.

FizzBuzz is a famous screening exercise, because it checks that you can use a loop, the modulo operator and ordered conditions together. It is short, but the order of the conditions trips people up, which is exactly why it is used.

You need for loops and if/elif/else. The test for "is a multiple of" is the same % trick used in the even or odd program: a number is a multiple of 3 when n % 3 == 0.

Quick facts
InputNone (or a limit such as 15 or 100)
OutputNumbers, with Fizz, Buzz or FizzBuzz replacing multiples
ApproachLoop and test % 15, % 3 and % 5 in a safe order
Time complexityO(n)

Approach (step by step)

For each number from 1 to the limit:

  1. If it is divisible by both 3 and 5 (equivalently by 15), print FizzBuzz.
  2. Else if it is divisible by 3, print Fizz.
  3. Else if it is divisible by 5, print Buzz.
  4. Else print the number itself.

The combined case has to come first. A multiple of 15 is also a multiple of 3 and of 5, so if you test 3 first, 15 prints only Fizz and the FizzBuzz branch is never reached.

How the code works

An if/elif/else chain runs only the first branch whose condition is true. That is what makes ordering so important: the most specific condition must be tested first. Using i % 3 == 0 and i % 5 == 0 (or i % 15 == 0) captures the special case.

The loop uses range(1, 101) so that 100 is included. For a quick test, a limit of 15 prints exactly one example of each kind of output, which is easy to verify by eye.

Python
for i in [3, 5, 15, 7]:
    print(i, i % 3 == 0, i % 5 == 0)

Variations: build the word instead of testing 15

A common alternative builds the text piece by piece: start with an empty string, add Fizz if the number is a multiple of 3, add Buzz if it is a multiple of 5, and print the number only when the string is still empty. No special case for 15 is needed, and adding a new rule (say 7 for Bazz) takes one more line.

You can generalise further by keeping the rules in a list of (divisor, word) pairs and looping over them. The result then works for any set of rules, which is a good demonstration of data-driven code, and it can be written in one line with a conditional expression when brevity matters more than readability.

Three ways to write FizzBuzz
VersionIdeaAdding a new rule
if / elif / elseTest 15, 3, 5 in orderMany more combinations needed
Build a stringAppend Fizz and Buzz, fall back to the numberOne extra if
Rules listLoop over (divisor, word) pairsOne extra pair, no code change

Examples you can run

Press Run, then edit the code and run it again.

Classic if/elif/else solution (1 to 15)

The FizzBuzz check is first. Changing 15 to 100 gives the full exercise.

Classic if/elif/else solution (1 to 15)
for i in range(1, 16):
    if i % 3 == 0 and i % 5 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)
Expected output1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

Build the word and use a rules list

Each rule appends its word when it applies, so there is no special case for 15. The extra Bazz rule for 7 needed only one more pair.

Build the word and use a rules list
rules = [(3, "Fizz"), (5, "Buzz"), (7, "Bazz")]
results = []
for i in range(1, 22):
    word = ""
    for divisor, text in rules:
        if i % divisor == 0:
            word += text
    results.append(word or str(i))
print(", ".join(results))
Expected output1, 2, Fizz, 4, Buzz, Fizz, Bazz, 8, Fizz, Buzz, 11, Fizz, 13, Bazz, FizzBuzz, 16, 17, Fizz, 19, Buzz, FizzBazz

Interactive version with a chosen limit

The limit comes from the user, and the output is joined on one line.

Interactive version with a chosen limit
limit = int(input("FizzBuzz up to: "))
out = []
for i in range(1, limit + 1):
    if i % 15 == 0:
        out.append("FizzBuzz")
    elif i % 3 == 0:
        out.append("Fizz")
    elif i % 5 == 0:
        out.append("Buzz")
    else:
        out.append(str(i))
print(" ".join(out))
Expected outputFizzBuzz up to: 10
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz

Common mistakes

Testing 3 and 5 before the combined case

✗ Wrong

for i in range(1, 16):
    if i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    elif i % 15 == 0:
        print("FizzBuzz")
    else:
        print(i)
15 prints Fizz; the FizzBuzz branch can never run.

✓ Right

for i in range(1, 16):
    if i % 15 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)

Put the % 15 (or % 3 and % 5) test first, because elif stops at the first match.

Stopping at 99 with range(1, 100)

✗ Wrong

for i in range(1, 100):
    print(i)
No error, but 100 (which should print Buzz) is missing.

✓ Right

for i in range(1, 101):
    print(i)

Use range(1, 101): the stop value is excluded.

Python FizzBuzz Program: questions people ask

How do you solve FizzBuzz in Python?

Loop from 1 to 100 and use if/elif/else: print FizzBuzz if the number is divisible by 15, Fizz if by 3, Buzz if by 5, otherwise the number.

Why must FizzBuzz check divisibility by 15 first?

Because a multiple of 15 is also a multiple of 3 and 5. If you test 3 first, the program prints only Fizz and never reaches the FizzBuzz case.

Can FizzBuzz be written without checking 15?

Yes. Build a string by adding Fizz for multiples of 3 and Buzz for multiples of 5, and print the number only if the string is empty.

Keep learning

Step-by-step lessons:
Python for LoopsPython if, elif and elsePython Logical Conditions

Related:
Python Program to Check Even or Odd NumberPython Program to Print a Multiplication TablePython Program to Check Prime NumberPython range() Function

More in number programs:
Python Program to Check Even or Odd NumberPython Program to Check Prime NumberPython Program to Find Factorial of a NumberPython Program to Print the Fibonacci SeriesPython Program to Check Armstrong NumberPython Program to Find GCD of Two NumbersPython Program to Check Leap YearPython Program to Find the Sum of Digits of a Number See all →