How to print in the same line in Python?
How to print in the same line in python
By default, the print function in Python takes us to the next line and prints the desired statement in the output.
In this article, we will discuss how we can print our statements in the same line in Python. We will try to understand the same with the help of some examples.
The following program illustrates how we use the print function in general. So, what we have done here is, we have stored two strings in two variables.
Later we used the print function to check the output.
a="Tutorials" b="Tutorials and Examples" print("The value of a is {}".format(a)) print("The value of b is {}".format(b))
INPUT-

OUTPUT-

In the above output, we have observed that the values of the variables a and b are printed in two separate lines.
In the next example, we will see how the 'end' keyword can help us to print both statements in a single line.
The following program illustrates the same-
a="Tutorials" b="Tutorials and Examples" print("The value of a is {}".format(a),end=" ") #to print in one line print("The value of b is {}".format(b))
INPUT-

OUTPUT-

In the above output, we have observed that the values of both variables are printed in the same line.
The next example is based on printing the elements of an array.
In the given program, we have declared an array, and then used ‘type’ and ‘ndim’ to check the data type and dimension of the object a.
And then finally we have used the for loop to print the elements of our array.
Following program illustrates how we can implement this-
import numpy as np a=np.array([11,12,13,14,15]) #declaring the array print(type(a)) print(a.ndim) #printing the array for i in range(5): print("The {} element of array is {}".format(i+1,a[i]))
INPUT-

OUTPUT-

In the above output, we have seen that-
- The data type is ‘numpy.ndarray’
- The dimension of our array is one.
- Then five statements are printed and each statement tells us about the element of the array.
In the following example we will again use the ‘end’ keyword and print all our elements in a single line.
Following program illustrates how we can perform this in Python-
import numpy as np a=np.array([11,12,13,14,15]) #declaring the array print(type(a)) print(a.ndim) #printing the array for i in range(1): print("The elements of array are {}".format(a[i]),end=" ") for j in range(1,5): print(a[j],end=" ")
INPUT-

OUTPUT-

This time in the output, our observations are-
- The data type is ‘numpy.ndarray’
- The dimension of our array is one.
- A single statement is printed that displays the elements of our array.
If you find the above code difficult to understand there’s an alternate approach that can implement the same.
import numpy as np a=np.array([11,12,13,14,15]) #declaring the array print(type(a)) print(a.ndim) for i in range(5): print(a[i],end=" ")
INPUT-

OUTPUT-

On executing the program, we can see that it displays the same result.
The examples that we have discussed till now take blank space as a parameter in the end.
We can also specify a special character at its place.
In the following examples, we shall see how we can display the output in a single line and add some special characters in between the elements.
In the given example, we have used the ‘#’ as a parameter.
Check the given program-
import numpy as np a=np.array([11,12,13,14,15]) #declaring the array print(type(a)) print(a.ndim) for i in range(5): print(a[i],end="# ")
INPUT-

OUTPUT-

In the above output, we have observed that-
- The data type is ‘numpy.ndarray’
- The dimension of our array is one.
- All the elements have the special character ‘#” after them.
Let us see how we can use some more special characters-
In the given example, we have used the ‘.’ as a parameter.
import numpy as np a=np.array([11,12,13,14,15]) #declaring the array print(type(a)) print(a.ndim) for i in range(5): print(a[i],end=". ")
INPUT-

OUTPUT-

In the above output, we have observed that-
- The data type is ‘numpy.ndarray’
- The dimension of our array is one.
- All the elements have the special character ‘.” after them.
In the given example, we have used the ‘*’ as a parameter.
import numpy as np a=np.array([11,12,13,14,15]) #declaring the array print(type(a)) print(a.ndim) for i in range(5): print(a[i],end="* ")
INPUT-

OUTPUT-

In the above output, we have observed that-
- The data type is ‘numpy.ndarray’
- The dimension of our array is one.
- All the elements have the special character ‘*” after them.
Now we will see how we can print a paragraph in Python-
In the first example, all the statements provided in the print function are displayed in separate lines.
Check the given program and see what happens when it is executed-
print("I love programming in Python language.") print("Lists, Tuples and Dictionaries are the data structures in Python.") print("We can create data frames in python by importing pandas.") print("Visualizations can be done for a better understanding of our data,") print("matplotlib and seaborn are the popular libraries for the same.")
INPUT-

OUTPUT-

All the statements are printed in separate lines in the output.
In the next program, we have used the ‘end’ keyword and printed the statements in the form of a paragraph-
print("I love programming in Python language.",end=" ") print("Lists,Tuples and Dictionaries are the data structures in Python.",end=" ") print("We can create dataframes in python by importing pandas.",end=" ") print("Visualizations can be done for a better understanding of our data,",end=" ") print("matplotlib and seaborn are the popular libraries for the same.")
INPUT-

OUTPUT-

In the above output, we have seen it is printed in the way we wanted.
So in this article, we discussed various examples in detail of how to print in the same line in Python.