Computer >> Computer tutorials >  >> Programming >> Python

How to avoid printing newline in Python?


The print() function in Python by default ends with newline. Python has a predefined format, so if we use print(variable name) then it will go to next line automatically.

For examples

print("Tutorial")
print("Tutorialspoint")

Output looks like this −

Tutorial
Tutorialspoint

But sometime it may happen that, we don't want to go to next line but want to print on the same line.

For Example

Input: print("Tutorial") print("Tutorialspoint")
Output: TutorialTutorialspoint

But here the solution depends on Python version.

It was possible in Python 2.x where print without newline was possible, if we write code in the above mentioned syntax.

Now consider the following code.

Example Code

print("Python Program"),
print("Tutorials Point")
# defining a list
my_arr = [11, 22, 33, 44]
# printing the list content
for i in range(4):
   print(my_arr[i]),

Output

Python Program
Tutorials Point
11
22
33
44

Now consider the following code where in output, printing has been done in the same line.

Example Code

print("Python Program",end =""),
print("Tutorials Point")
# defining a list
my_arr = [11, 22, 33, 44]
# printing the list content
for i in range(4):
   print(my_arr[i], end =""),

Output

Python ProgramTutorials Point
11223344