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

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.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.