Draw a Fish with Stars in Python – ASCII Pattern
This Snippet is coded by a user inOnline python Compiler. You can see and run this code too.
زبان: python
This snippet is a compact, beginner-friendly example of ASCII art in Python that prints a fish shape to the console using nothing but a for loop and the print function — no external libraries required.
How the code works
- The outer loop
for i in range(11)builds eleven rows of the fish. Each row is a mix of spaces (empty) and asterisks (filled). - For every row,
empty = abs(5-i) * 4is computed so the body is widest at the middle row and tapers toward the top and bottom. - The list
patternstores alternating lengths of empty and filled segments. The inner loop then prints spaces for even indices and stars for odd indices. - Rows
i == 2andi == 3are handled specially to render the fish eye as a separate block of stars. - Row
i == 5is overwritten to open the fish mouth by removing part of the filled area. - At the end of each row,
tail_emptyandtail_fillare appended to the pattern list, drawing the tail with a width that grows with distance from the center row.
How to use it
Save the code as fish.py and run it with python fish.py. The artwork is printed to the terminal; use a monospace font for the best result.
Customization tips
- Change the value
11in the range to make the fish larger or smaller. - Adjust the multipliers
4and8to widen or narrow the body and tail. - The inner loop reuses the variable name
i, which shadows the outer counter; renaming it to something likeidximproves readability and avoids confusion. - The
i == 5branch relies on theemptyvalue left over from the previous row, so computing it explicitly is safer.
Overall, it is a great exercise for practicing nested loops, index arithmetic and algorithmic thinking.