How to Print in the Same Line in Python
To print in Python, we use the print () function and the syntax of print () goes like this:
print (values, sep = '', end = '\n', file = file, flush = flush)
- end : It is an optional parameter. It is used to specify the character to be printed at the end. Default value is a new line character – end = ‘\n’.
By default, in Python, when we write a print function and it is executed immediately, the cursor moves to the next line. If we write 2 print () functions, they will be printed in two different lines.
In other programming languages like C and C++, it is not like this; It means, if we write multiple printf () functions they will all be continued in the same line. We need to use \n to switch to the next line.
In this article, we will discuss a few ways in Python to stop printing and transferring cursor to the next line and print continuously in the same line.
First, let us see how normally print () function executes?
print ("Hello") print ("World")
Now, to get Hello World in the output:
1. Using the end parameter in print ():
As we discussed at the top of the article,
The end parameter specifies the character to be printed at the end. Default value is a new line character – end = '\n'.
We can assign an empty character or a space to the end parameter.
print ("Hello", end = ' ') print ("World")
2. Using a comma(,) : (Python 2. x)
Program: print("Hello"), print("World") arr = [1, 2, 3, 4] for i in range(4): print (arr [i]),
Hello World 1 2 3 4
This only works in the old version – Python 2. x in Python. Using a comma after the print () statement can avoid going to the next line to print the next statement.
3. End parameter while looping
list1 = [1, 2, 3, 4] for i in list1: print (i, end = " ")
[1, 2, 3, 4]
We kept the print statement in a for loop. We assigned the end parameter with a space. For all iterations of i, the print is executed. i is printed, and the end is updated to space, thus not transferring to a new line but taking a space.
4. Using the *:
list1 = [1, 2, 3, 4] print (*list1) string1 = "Hello world" print (*string1)
1 2 3 4 H e l l o w o r l d
Using the * before printing the string or a list, we can print the elements or the characters of a list or a string separated by a space rather than using loops.
5. Concatenation ( only for sequence type data types)
a = "Hello" b = "world" print (a + " " + b) c = [1, 2, 3] d = [4, 5] print (c + d)
Hello world [1, 2, 3, 4, 5]
We have a concatenation method for sequence type data types like strings and lists, and + is called the concatenation operator. We can add or concatenate two separate elements ( strings or lists) using this method.
Example program with all ways:
string1 = input ("Enter a string: ") string2 = input ("Enter another string: ") print (string1 + " " + string2) print (string1 + string2) print (string1, end = " ") print (string2) print (* (string1 + string2))
Enter a string: Hello Enter another string: World Hello World HelloWorld Hello World H e l l o W o r l d