View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Generally, people switching from C/C++ to Python wonder how to print two or more variables or statements without going into a new line in python.
Since the python print[] function by default ends with a newline. Python has a predefined format if you use print[a_variable] then it will go to the next line automatically.
For example:
Python3
print
[
"geeks"
]
print
[
"geeksforgeeks"
]
Will result in this:
geeks geeksforgeeks
But sometimes it may happen that we don’t want to go to the next
line but want to print on the same line. So what we can do?
For Example:
Input : print["geeks"] print["geeksforgeeks"] Output : geeks geeksforgeeks Input : a = [1, 2, 3, 4] Output : 1 2 3 4
The solution discussed here is totally dependent on the python version you are using.
Print without newline in Python 2.x
python
print
[
"geeks"
],
print
[
"geeksforgeeks"
]
a
=
[
1
,
2
,
3
,
4
]
for
i
in
range
[
4
]:
print
[a[i]],
Output:
geeks geeksforgeeks 1 2 3 4
Print without newline in Python 3.x
python3
print
[
"geeks"
, end
=
" "
]
print
[
"geeksforgeeks"
]
a
=
[
1
,
2
,
3
,
4
]
for
i
in
range
[
4
]:
print
[a[i], end
=
" "
]
Output:
geeks geeksforgeeks 1 2 3 4
Print without newline in Python 3.x without using for loop
Python3
Output:
1 2 3 4 5 6
The print[] method in Python automatically prints in the next line each time. The print[] method by default takes the pointer to the next line.
Example
Live Demo
for i in range[5]: print[i]
Output
0 1 2 3 4
Modify print[] method to print on the same line
The print method takes an extra parameter end=” “ to keep the pointer on the same line.
The end parameter can take certain values such as a space or some sign in the double quotes to separate the elements printed in the same line.
Syntax
print[“…..” , end=” “]
Print on same line with space between each element
The end=” “ is used to print in the same line with space after each element. It prints a space after each element in the same line.
Example
Live Demo
for i in range[5]: print[i,end=" "]
Output
0 1 2 3 4
Print on same line without space between elements
The end=”” is used to print on same line without space. Keeping the doube quotes empty merge all the elements together in the same line.
Example
Live Demo
for i in range[5]: print[i,end=""]
Output
01234
Print on same line with some sign between elements
The end=”,” is used to print in the same line with a comma after each element. We can use some other sign such as ‘.’ or ‘;’ inside the end parameter.
Example
Live Demo
for i in range[5]: print[i,end=","] print[i,end="."]
Output
0,1,2,3,4, 0.1.2.3.4.
Updated on 10-Mar-2021 14:07:42
- Related Questions & Answers
- How to print new line in Java?
- How to Pretty print Python dictionary from command line?
- How to print a blank line in C#?
- How to print matrix without line numbers in R?
- How can we combine multiple print statements per line in Python?
- How to capture multiple matches in the same line in Java regex
- How to create two line charts in the same plot in R?
- Print level order traversal line by line in C++ Programming.
- How to print pattern in Python?
- Print new line and tab in Arduino
- How to compare two different files line by line in Python?
- How to execute Python multi-line statements in the one-line at command-line?
- How to print a line on the console using C#?
- How to use GridBagConstraints to layout two components in the same line with Java
- How to print concatenated string in Python?