The Python's print() function is used to print the result or output to the screen. By default, it jumps to the newline to printing the next statement. It has a pre-defined format to print the output. Let's understand the following example. Show
Example - 1 Output: Or, we can write the complete statement in single print() function. Output: The Python print() function has an argument called end, which prevents jump into the newline. Let's understand the following example. Example - 3: Output: In the above code, we declared a list and iterated each element using for loop. The print() function printed the first element of the list and then printed the end value which we assigned as ' ' whitespace and it will be printed till the element of the list. We can assign any literal to the end. Let's understand the following example. Example - 4 Output: 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. ExampleLive Demo for i in range(5): print(i) Output0 1 2 3 4 Modify print() method to print on the same lineThe 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. Syntaxprint(“…..” , end=” “) Print on same line with space between each elementThe 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. ExampleLive Demo for i in range(5): print(i,end=" ") Output0 1 2 3 4 Print on same line without space between elementsThe end=”” is used to print on same line without space. Keeping the doube quotes empty merge all the elements together in the same line. ExampleLive Demo for i in range(5): print(i,end="") Output01234 Print on same line with some sign between elementsThe 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. ExampleLive Demo for i in range(5): print(i,end=",") print(i,end=".") Output0,1,2,3,4, 0.1.2.3.4.
Updated on 10-Mar-2021 14:07:42
As someone who teaches a lot of beginner programming content, I occasionally stumble upon questions like “how do you print on the same line in Python?” Luckily, I have an answer to that! In short, there are two main ways to print on the same line in Python. For Python 2, use the following print syntax: Table of ContentsProblem IntroductionIn many programming languages, printing on the same line is typically the default behavior. For instance, Java has two command line print functions: System.out.print(); System.out.println(); As you can probably imagine, the default Of course, if the SolutionsIn order to print on the same line in Python, there are a few solutions. Unfortunately, not all of the solutions work in all versions of Python, so I’ve provided three solutions: one for Python 2, another for Python 3, and a final solution which works for both. Print on the Same Line The Old WayWhen I was searching for solutions to this problem, I found a lot of material on Python 2 which is quickly phasing out (I hope). That said, I felt this solution would be helpful to anyone still rocking it. At any rate, when you print something in Python 2, the syntax is the same as Python 3, but you leave out the parentheses: print "Live PD" Of course, in both cases, the default behavior is to print with a newline. As a result, we’ll need to add a clever bit of syntax—a comma: print "Live PD", Now, the print function should exclude the newline.
However, this solution will add an extra space to the end of the string. Also, you may notice that this solution does not print immediately. If that happens, you can make a call to Print on the Same Line with the Write FunctionFortunately, we can bridge the gap between Python 2 and 3 using a function out of the import sys sys.stdout.write("Breaking Bad") Again, since there is no newline, you may need to flush the buffer to see any results: import sys sys.stdout.write("Breaking Bad") sys.stdout.flush() In either case, this solution will get the job done in both versions of Python. Print on the Same Line the New WayIn Python 3, print("Mob Psycho 100", end="") And, that’s it! Instead of the string ending in a newline, it will end in an empty string. Of course, this solution comes with the same caveat as the other previous two solutions: you may need to flush the buffer. PerformanceAs always, I like to take a look at all the solutions from the point of view of performance. To start, I usually store each solution in a string. To avoid excessive printing during the test, I’ve chosen to write empty strings: setup=""" import sys """ write_solution = """ sys.stdout.write("") """ print_solution = """ print("", end="") """ Unfortunately, I was unable to test the Python 2 solution on my system, so feel free to share your results in the comments. At any rate, I like to use the timeit library for a quick and dirty performance test: >>> import timeit >>> min(timeit.repeat(stmt=write_solution, setup=setup, repeat=10)) 0.20978069999999605 >>> min(timeit.repeat(stmt=print_solution, setup=setup, repeat=10)) 0.5292953999999952 Clearly, the A Little RecapWell, that’s it for this one. Check out the code block below for a list of all the solutions: # Python 2 only print "Live PD", # Backwards compatible (also fastest) import sys sys.stdout.write("Breaking Bad") # Python 3 only print("Mob Psycho 100", end="") As always, if you know any other ways to print on the same line in Python, let us know in the comments. In the meantime, why not grow your Python knowledge with the following articles:
If you liked this article or any of the ones I listed, consider sticking around long term by becoming a member of the community or hopping on the mailing list.While you’re here, why not take advantage of some these Python books: Otherwise, I appreciate the support. Thanks for stopping by! How do you update the same line in Python?Use a carriage return "\r" to print over the same line
Use the syntax print(string, end = "\r") to make the next stdout line begin at the beginning of the current line. As a result, string is overwritten by the following print() statement.
How do I keep the print on the same line in Python?If you want to print your text on the same line in Python 2, you should use a comma at the end of your print statement. Here's an example of this in action: print "Hello there!", print "It is a great day."
How do you overwrite a previous printed line in Python?Summary: The most straightforward way to overwrite the previous print to stdout is to set the carriage return ( '\r' ) character within the print statement as print(string, end = "\r") . This returns the next stdout line to the beginning of the line without proceeding to the next line.
What does '\ r do in Python?In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.
|