Skip to content

How to print numbers in same line in Python | Example code

  • by

Put end = ”parameter at the end of the print method, to works print numbers in the same line in Python.

Note: default python’s print() function ends with a newline.

Example code print numbers in the same line in Python

Simple python example code Printing numbers in the same line.

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for x in list1:
    print(x, end=" ")

Output:

How to print numbers in same line in Python

Print range of numbers on same line

Example code in Python 3. You can provide any delimiter to the end field (space, comma, etc.)

for x in range(1, 11):
    print(x, end=" ")

Output: 1 2 3 4 5 6 7 8 9 10

Or

Python one-liner to print the range example.

print(*range(1, 11))

Do comment if you have any doubts and suggestions on this topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading