Print a string 10 times

EasyFunction Programs · 2 of 39

Write a Python function that prints a string 10 times.

Advertisement
Input
"sqa"
Output
sqa sqa sqa sqa sqa sqa sqa sqa sqa sqa 
hi hi hi 

Approach

  1. Loop 10 times inside the function; make the count a default parameter.

Solution

Python
def repeat(text, times=10):    for _ in range(times):        print(text, end=" ")    print() repeat("sqa")repeat("hi", 3)
Output
sqa sqa sqa sqa sqa sqa sqa sqa sqa sqa 
hi hi hi 

Line by line

Line 1

times=10 is a default value, used when the caller leaves it out.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs