GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — Python end (end=) Parameter in print()
Python end (end=) Parameter in print()This article is created to cover an interesting topic in Python: the "end" parameter of print(). Many Python programs use the end (end=). As a result, we must comprehend it. The end parameter is used to change the default behavior of the print() statement in Python. That is, since print() automatically inserts a newline after each execution. As a result, using the end can cause this to change. The end parameter is sometimes used to customize the output console. Benefits of the end parameter in PythonWe can use "end" to skip the insertion of an automatic newline. But we can also use it to insert one, two, or multiple newlines using a single "print()." The examples given below show the use of "end" in every aspect. Python end Parameter SyntaxBecause "end" is a parameter of the print() statement or function, its syntax is not unique. That is, the syntax to use "end" looks like this: print(end="") Inside the double quote, you can insert whatever you want, according to your needs, such as a space, no space, comma, newline ('\n'), etc. Python end parameter exampleThis section includes a few examples that will help you fully understand the topic. Let's start with a very basic one, given below. end parameter without a space or newlineThis program employs "end," which results in no space and no newline. print("Py", end="") print("thon") The above program produces the output shown in the snapshot given below: parameter ending with a space and no newlineThe program given below shows how to add a single or multiple spaces with no newline in a Python program: print("This is a tutorial on", end=" ") print("\"end\" parameter.") The snapshot given below shows the sample output produced by the above program: end parameter with a comma, a space, and no newlineThis is the third example of a program with an "end" parameter. This program demonstrates how to use "end" to add multiple items: print("Hello", end=", ") print("Programmer") This program produces: Note: Basically, whatever you provide inside the double quote of end="". You will get the respective output. end parameter with one, two, or multiple newlinesSince the print() statement, by default, always inserts and prints a single newline, but using "end" as its parameter, we can change this to print the required number of newlines, as shown in the program given below: print("Hey", end="!\n\n") print("What's Up", end="\n") print("Great to see you learning Python here.", end="\n\n\n\n") print("Thank You!") Now the output produced by this Python program is exactly the same as shown in the snapshot given below: You can also put some message or content for print() inside its "end" parameter, like shown in the program given below: print("H", end="ey!\n\n") print("What", end="'s Up\n") print("Great to see you ", end="learning Python here.\n\n\n\n") print("Thank", end=" You!\n") This program produces the same output as the previous program's output. Putting everything from print() inside the end parameterYou can also put all the things inside the "end," like shown in the program given below: print(end="Hey!\n\n") print(end="What's Up\n") print(end="Great to see you learning Python here.\n\n\n\n") print(end="Thank You!\n") Again, this program produces the same output as the second previous program. Note: Basically, the "end" parameter of print() forces it not to automatically insert a newline. end parameter provides interactive output for user inputThis type of program, where the "end" parameter is used when asking the user to enter some inputs, may be seen a lot of times to receive the input on the same line, where the asking message is written as shown in the program given below: print("Enter anything: ", end="") val = input() print("You've entered:", val) Here is its sample run with user input codescracker: To print list items in a single line, use the end parameterThis is the task, where we almost want to use "end" every time. Because when we've a list of large number of elements, then it takes large number of lines, that looks weird sometime. As a result, we can use "end" to modify it as shown in the program below: nums = [1, 2, 3, 4, 5] for n in nums: print(n, end=" ") This program produces: « Previous Tutorial Next Tutorial » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |