[Python] Python Fireworks with Single Loop and fstrings[Python] Python Fireworks with Single Loop and fstrings

🎆

Happy New Year

Week 1, 2026

def fireworks(width: int): # Initialise container variables for the top, middle and bottom of the star top:str = "" middle:str = "-" * width bottom:str = "" # We use the half distance as we work on half of the star at a time half:int = width // 2 # Loop through each row on the star for i in range(half): # The outer margin at the top, inner space at the bottom shrinking_gap = " " * (half - (i + 1)) # The inner space at the top, outer margin at the bottom growing_gap = " " * i # Append the strings and gaps together to generate the image halves # NOTE: \n means newline, we need to add it to the end of each line. top += f"{growing_gap}\{shrinking_gap}|{shrinking_gap}/\n" bottom += f"{shrinking_gap}/{growing_gap}|{growing_gap}\\\n" # Output the halves in order with the middle string # NOTE: Be sure to strip the final \n to ensure there is no extra spaces print(top.strip("\n")) print(middle) print(bottom.strip("\n")) fireworks(99)