Home > OS >  How to print without a newline without using end=" "?
How to print without a newline without using end=" "?

Time:11-01

I need to design a function named firstN that, given a positive integer n, displays on the screen the first n integers on the same line, separated by white space.

An example of using the function could be:

>>> firstN(10)
0 1 2 3 4 5 6 7 8 9

I have done this:

def firstN(n):
    for i in range(10):
        print (i, end=" ")
firstN(10);

but I can't put end=" " because my teacher has a compiler that doesn't allow it

CodePudding user response:

You can use starred expression to unpack iterable arguments:

def firstN(n):
    print(*(range(n)))

firstN(10)

# 0 1 2 3 4 5 6 7 8 9

CodePudding user response:

Thanks for your answers. Yes, my teacher was using the version 2 of python. I have alredy solved the question. To print without a newlineyou need to put a comma:

def firstN(n):
    for i in range(10):
        print (i,)
firstN(10);
  • Related