Python is one of the most widely used programming languages. There are many reasons for us to use python but its syntax is one of the reasons that make it very easy and simple to use. We can write python code without using semicolons and declaring data types for variables. This solves many errors while running the code. The printing part of python is much easier than any other language. There is just a simple print function for it. In this article, we will discuss how to print without newline in python.
Python print() function
To print the data, we use the print function. It is very easy to use. Let’s have a look at one example of the print function.
print("first line") print("second line") print"third line")
The output is:
We have to pass the data that we wish to print in the print function. It is this easy to print data in python. But observe the output of the above python code. We used three print functions and each value is printed in a different line. There is no problem with it but sometimes we want that data should be printed on the same line. It solely depends upon the version of python we are using.
Python print without newline
Printing without newline is also very easy in python. We just have to use the end keyword for it. Let’s discuss this with a simple example.
print("first line",end=" ") print("second line",end=" ") print("third line",end=" ")
Similar to the earlier code, we used three print functions. But this time in each function, we also passed a second argument. We passed the end keyword. The end keyword has a special function. It specifies how the printed data will end. If we give it a value of a single space, it will print the data without the next line. The output of the code will verify it.
The output is:
Similarly, we can use the end keyword to print the values of an array(basically a list in python) without newlines. We use a loop to print the values of an array and each value is always printed in the next line if the end keyword is not used. Let’s have a look.
arr = [1,2,3,4,5] for value in arr: print(value)
The output is:
Observe the output. Although we used only one print function, due to iteration, it will be called multiple times. We can see each value is printed in different lines. We can print all the values in one line using the end keyword, similarly, we used earlier.
arr = [1,2,3,4,5] for value in arr: print(value, end=" ")
The output is:
Observe the output now. Each value is printed in the same line only. This is how the end keyword works.
The method we discussed earlier is supported in python 3.0 and above. For the python version 2.0 and above, we have to use a different approach. Instead of using the end keyword, We can simply separate the print function by a comma to print without a newline.
print("firstline"), print("secondline"), print("thirdline")
The output is:
Remember, this approach only works in the python version 2.0 and above, but less than 3.0.
Conclusion
The syntax of python is very simple and easy. This is a plus point for python. We can print data very easily using the print function. In this article, we discussed how to print data without a newline. It depends upon the version we are using. For version 2.x, we can simply separate print function by a comma to print data without a newline. For version 3.x, we have to use the end keyword and set its value as a single space.