Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (16.4k points)
edited by

Hello, all. I just want to print the looped output on the display on the same line? 

In Python 3.x, how can I do this in a simpler way?

I couldn't able to find the solution for python 3.x

i = 0 

while i <10:

     i += 1 

     ## print (i) # python 2.7 would be print i,

     print (i) # python 2.7 would be 'print i,'

Output:

1

2

3

4

5

6

7

8

9

10

But, I actually want is:

12345678910

1 Answer

0 votes
by (26.4k points)

Using help(print):

Help on built-in function print in module builtins:

print(...)

    print(value, ..., sep=' ', end='\n', file=sys.stdout)

    Prints the values to a stream, or to sys.stdout by default.

    Optional keyword arguments:

    file: a file-like object (stream); defaults to the current sys.stdout.

    sep:  string inserted between values, default a space.

    end:  string appended after the last value, default a newline.

Here, you can use end keyword:

>>> for i in range(1, 11):

...     print(i, end='')

... 

12345678910>>> 

Want to learn more about Python, Come & Join: Python course

Related questions

0 votes
1 answer
asked Jan 5, 2021 in Python by laddulakshana (16.4k points)
0 votes
2 answers
0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...