Table of contents
1.
Introduction
2.
What is the Python End Parameter?
2.1.
Python
3.
Examples of Python End Parameter:
3.1.
Example 1: Printing on the same line
3.2.
Python
3.3.
Example 2: Creating a custom separator
3.4.
Python
3.5.
Example 3: Printing without a newline
3.6.
Python
4.
Frequently Asked Questions
4.1.
Can the "end" parameter be used with other functions besides print()?
4.2.
What happens if you don't specify the "end" parameter?
4.3.
Can you use escape sequences with the "end" parameter?
5.
Conclusion
Last Updated: Jun 29, 2024
Easy

End in Python

Introduction

Python is a popular programming language known for its simplicity & versatility. One of the essential features of Python is the ability to control the output of strings using the "end" parameter. 

End in Python

In this article, we will discuss what the "end" parameter is & how it can be used to format the output of strings in Python. We will provide examples to show how to use this feature.

What is the Python End Parameter?

The "end" parameter in Python is used to specify the character that should be printed at the end of a string when using the print() function. By default, the print() function adds a newline character (\n) at the end of each printed string, which causes the next output to appear on a new line.

However, by using the "end" parameter, you can change this default behavior & specify a different character or string to be printed at the end of the output. This allows you to control the formatting of the output & customize it according to your needs.

The syntax for using the "end" parameter is as follows:

print("string", end="character")


Here, "string" represents the string you want to print, & "character" is the character or string you want to be printed at the end of the output instead of the default newline character.

For example, if you want to print multiple strings on the same line separated by a space, you can use the "end" parameter like this:

  • Python

Python

print("Hello,", end=" ")

print("world!")
You can also try this code with Online Python Compiler
Run Code

 

Output:

Hello, world!


In this case, the first print() statement uses the "end" parameter to specify a space character instead of the default newline. As a result, the second print() statement continues on the same line, separated by a space.

The "end" parameter gives you flexibility in formatting your output & can be used in various scenarios to achieve the desired output structure.

Examples of Python End Parameter:

Now, let’s look at the few examples to understand this better: 

Example 1: Printing on the same line

  • Python

Python

print("Hello,", end=" ")

print("how are you?")
You can also try this code with Online Python Compiler
Run Code

 

Output

Hello, how are you?


In this example, the first print() statement uses the "end" parameter to specify a space character instead of the default newline. As a result, the second print() statement continues on the same line, separated by a space.

Example 2: Creating a custom separator

  • Python

Python

print("apple", end="-")

print("banana", end="-")

print("orange")
You can also try this code with Online Python Compiler
Run Code

 

Output

apple-banana-orange


Here, we use the "end" parameter to specify a hyphen (-) as the separator between the strings. The output is printed on the same line, with the strings separated by hyphens.

Example 3: Printing without a newline

  • Python

Python

print("Enter your name:", end=" ")

name = input()

print("Hello,", name)
You can also try this code with Online Python Compiler
Run Code

 

Output

Enter your name: Rahul
Hello, Rahul


In this example, we use the "end" parameter to print the prompt message "Enter your name:" without a newline. The input() function continues on the same line, allowing the user to enter their name right after the prompt. Finally, the entered name is printed on a new line.

Frequently Asked Questions

Can the "end" parameter be used with other functions besides print()?

No, the "end" parameter is specific to the print() function in Python.

What happens if you don't specify the "end" parameter?

If you don't specify the "end" parameter, Python will use the default newline character (\n) at the end of each print() statement.

Can you use escape sequences with the "end" parameter?

Yes, you can use escape sequences like \t (tab), \n (newline), etc., with the "end" parameter to format the output.

Conclusion

In this article, we have learned about the "end" parameter in Python & how it can be used to control the formatting of the output when using the print() function. We discussed few examples that demonstrated how to print strings on the same line, create custom separators, & print without a newline. With the help of the "end" parameter, you can enhance the readability & presentation of your program's output drastically.

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass