In this short guide, I’ll show you different techniques for how to print on same line in python. So without further ado let’s get started.
1. By Using end Argument
The print is the builtin function in python used to print the program output to the screen. By default, it prints a newline character in the end.
It accepts an argument ‘end’ with the default value of \n.
To print on the same line we can set the value of ‘end’ to the empty string.
# Program to Print On Same Line In Python string1 = "Hello I am" string2 = " On Same Line" print(string1,end = '') print(string2,end = '')
Here is the output of above code.
2. By Using sys Module
We can also use the sys.stdout.write a function from the sys module to print on the same line.
import sys string1 = "Hello I am" string2 = " On Same Line" sys.stdout.write(string1) sys.stdout.write(string2)
Hello I am On Same Line
Also read about how to remove newline character from string