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

If you are print multiple times then it will not print output in the same line in Python. Because print() function is used to print the output and By default, it jumps to the newline to printing the next statement.

See the below example.

print("Hello")
print("world")
print("!")

Output:

How to print in the same line in Python

To print in the same line write a complete statement in a single print() function in Python.

print("Hello world !")

Output: Hello world !

Use end keyword

By adding the ‘ ‘ two single quotation marks you create the space between the two words, Hello and World, (Hello’ ‘World’) – I believe this is called a “blank string”.

print('Hello', end= ' ')
print('World')

Output: Hello World

Print in the same line in for loop

The Python print() function has an argument called end, which prevents jump into the newline. Example use the end keyword in python. SAMPLE Of “end=” being used to remove the “newline” character.

list1 = [10, 11, 12, 13, 14, 15]
for i in list1:
    print(i, end=" ")

Output:

Print in the same line in for loop

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.