Back to Python
2026-01-175 min read

Example 2: Python print() with end Parameter

Learn Example 2: Python print() with end Parameter step by step with clear examples and exercises.

Title: Mastering Python's print() Function with the end Parameter

Why This Matters

The print() function is one of the most frequently used built-in functions in Python. Understanding its end parameter is crucial for controlling output formatting and creating more sophisticated programs. This knowledge is essential for acing programming interviews, debugging real-world issues, and writing cleaner, more efficient code.

A well-formatted output can significantly improve the readability of your program, making it easier for others (and yourself) to understand what your code is doing. By mastering the end parameter in Python's print() function, you will be able to customize your output to meet specific needs and expectations.

Prerequisites

Before diving into the print() function's end parameter, you should be familiar with:

  • Basic Python syntax
  • Variables and data types
  • Control structures (e.g., if-else, for loops, while loops)
  • Basic input/output operations (e.g., print(), input())
  • Understanding lists and tuples in Python
  • Familiarity with string formatting and interpolation techniques such as f-strings or the format() function

Core Concept

The print() function is a versatile built-in function in Python that outputs one or more values to the console. By default, it adds a newline character (\n) at the end of each output. However, you can customize this behavior using the end parameter.

Here's an example demonstrating the default behavior:

print("Hello")
print("World")

Output:

Hello
World

Now, let's modify the output by setting the end parameter to a space instead of a newline:

print("Hello", end=" ")
print("World", end=" ")
print() # Adding an empty print statement to create a new line at the end

Output:

Hello World

By doing so, we have eliminated the extra newline between "Hello" and "World". You can set end to any string you'd like, not just a space or newline. For example, if you want to print multiple lines without any extra spaces, use an empty string ("") as the value for end.

Customizing Output Formatting with sep and end Parameters

In addition to the end parameter, Python's print() function also has a sep parameter that allows you to customize the separator between items in a list or tuple:

numbers = [1, 2, 3, 4, 5]
print(numbers) # Default output: [1, 2, 3, 4, 5]
print(numbers, sep=", ") # Custom output: 1, 2, 3, 4, 5

The sep parameter can be particularly useful when dealing with large amounts of data or complex structures like lists and tuples. By customizing the separator, you can make your output more readable and easier to interpret.

Using the end Parameter for Progress Bars and Animations

Another practical application of the end parameter is creating progress bars or animations in Python. By updating the value of end periodically, you can create visual feedback that helps users understand the status of a long-running process:

import time

def print_progress(completion):
bar = "|" * int(completion * 20) + " " * (40 - int(completion * 20))
print(f"\rProgress: {bar}", end="\r")

for i in range(1, 101):
print_progress(i / 100)
time.sleep(0.1)

In this example, the print_progress() function updates the progress bar by setting the value of end to a new string that represents the current progress. The \r character is used to overwrite the previous line instead of appending to it.

Worked Example

Let's create a simple program that calculates and prints the sum of two numbers with custom output formatting using both sep and end parameters:

def calculate_sum(num1, num2):
result = num1 + num2
print(f"The sum of {num1} and {num2} is {result}", sep=" ", end=".\n\n")

Test the function with some example numbers

calculate_sum(3, 5)

calculate_sum(-2, 7)


Output:

The sum of 3 and 5 is 8 .

The sum of -2 and 7 is 5 .


In this example, we use the `sep` parameter to add a space between the numbers and the operator, and the `end` parameter to add a period and a newline at the end of each print statement. This results in a cleaner, more readable output.

Common Mistakes

  1. Not specifying the end parameter: If you forget to set the end parameter, the default newline character will be added after each print statement, which may cause unexpected formatting issues in your output.
  1. Incorrectly setting the end parameter: Be mindful of what you set the end parameter to. For example, if you want to print multiple lines without any extra spaces, use an empty string ("") as the value for end.
  1. Misunderstanding the purpose of the sep parameter: The sep parameter is used to specify the separator between each item in a list or tuple when printing. It's easy to confuse this with the end parameter, but they serve different purposes.
  1. Not handling lists and tuples correctly: When using the sep parameter with lists and tuples, make sure that all items are separable by the specified separator. Otherwise, you may encounter errors or unexpected output.
  1. Using the sep parameter with strings: The sep parameter is only applicable when printing lists and tuples. To separate words in a string, you can use spaces or other delimiters as needed.

Practice Questions

  1. Write a program that prints the sum of three numbers using the print() function's sep and end parameters.
  2. Modify the worked example to print the product of two numbers instead of their sum, using custom output formatting with both sep and end.
  3. Write a program that reads two numbers from the user, calculates their sum, and prints the result with a custom separator between the numbers and the sum.
  4. Create a program that takes a list of numbers as input, calculates the average, and prints the result using custom output formatting with both sep and end.
  5. Write a program that reads a line of text from the user, replaces all occurrences of "Hello" with "Greetings," and prints the modified text using custom output formatting with both sep and end.

FAQ

  1. What is the default value for the end parameter in Python's print() function? The default value for the end parameter is a newline character (\n).
  2. Can I set the end parameter to any string in Python's print() function? Yes, you can set the end parameter to any string you'd like, not just a space or newline.
  3. What is the purpose of the sep parameter in Python's print() function? The sep parameter is used to specify the separator between each item in a list or tuple when printing. It's different from the end parameter, which controls what appears at the end of each print statement.
  4. How can I handle lists and tuples correctly when using the sep parameter? To handle lists and tuples correctly with the sep parameter, make sure that all items are separable by the specified separator. If necessary, convert non-separable items (e.g., strings) to a list or tuple before printing.
  5. Can I use the sep parameter with strings in Python's print() function? No, the sep parameter is only applicable when printing lists and tuples. To separate words in a string, you can use spaces or other delimiters as needed.
Example 2: Python print() with end Parameter | Python | XQA Learn