Print Line of N Stars Recursively

Print n stars in a line using recursion.

Logic BuildingIntermediate
Logic Building
def print_stars(n):
    # Base case
    if n == 0:
        return
    
    # Recursive case
    print("*", end="")
    print_stars(n - 1)

# Test
n = int(input("Enter n: "))
print_stars(n)
print()

Output

Enter n: 5
*****

Print one star, then recurse for n-1 stars.

Key Concepts:

  • Base case: n == 0, return
  • Print one star
  • Recurse with n-1