Python Practice › Function Programs › Program 2
Print a string 10 times
Write a Python function that prints a string 10 times.
Input
"sqa"
Output
sqa sqa sqa sqa sqa sqa sqa sqa sqa sqa hi hi hi
Approach
- Loop 10 times inside the function; make the count a default parameter.
Solution
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.
Learn the concept
New to this topic? Read the lesson, then come back.