Is this a right code to print x for n times?

Is this a right way to write a function that prints x for n times?
My code:

def funz(x,n):
    f = (x+"\n")*(n-1) + x
    print(f)

>Solution :

You can use sep argument of print:

def funz(x, n):
    print(*[x]*n, sep='\n')

funz('a', 3)

# Output:
a
a
a

Step by step:

>>> print([x])
['a']

>>> print([x]*n)
['a', 'a', 'a']

>>> print(*[x]*n)
a a a

>>> print(*[x]*n, sep='\n')
a
a
a

Leave a Reply