Apa itu end di python?


The print() function in python always creates a newline. But there is also a parameter for this function which can put other characters instead of new line at the end. In this article we will explore various options for this parameter.

Example

In the below example we see various ways we can assign values to the end parameter and see the result from it.

 Live Demo

print("Welcome to ")
print("Tutorialspoint")

print("Welcome to ", end = ' ')
print("Tutorialspoint")

print("emailid",end='@')
print("tutorialspoint.com")

Output

Running the above code gives us the following result −

Welcome to
Tutorialspoint
Welcome to Tutorialspoint

Apa itu end di python?

Updated on 22-Jul-2020 08:30:56

  • Related Questions & Answers
  • Python file parameter in print()?
  • How to print size of array parameter in a function in C++?
  • Print a list in Python
  • Print a Calendar in Python
  • Print Words Vertically in Python
  • HTML DOM Parameter Object
  • How to set default parameter values to a function in Python?
  • Print all Subsequences of String which Start with Vowel and End with Consonant in C++
  • What is End-to-End Encryption (E2EE)?
  • Remove Nth Node From End of List in Python
  • What is final parameter in Java
  • Parameter Passing Techniques in C/C++
  • In-place Move Zeros to End of List in Python
  • Print Colors of terminal in Python
  • How to print pattern in Python?

We all have written multiple print statements in Python with or without parameters. But have you ever wondered the significance of using sep and end parameters to the print statement?

In this post, we will discuss how ‘sep’ and ‘end’ parameters can be used to change the way in which the contents of the print method are printed on the console.

1. The end parameter

The end parameter is used to append any string at the end of the output of the print statement in python.

By default, the print method ends with a newline. This means there is no need to explicitly specify the parameter end as '\n'. Programmers with a background in C/C++ may find this interesting.

Let us look at how changing the value of the end parameter changes the contents of the print statement onscreen.

The below example demonstrates that passing '\n' or not specifying the end parameter both yield the same result. Execute 2 lines of code at a time to see the result.

print("Studytonight",)
print("is awesome")

print("Studytonight", end= "\n")
print("is awesome")

Output:

Studytonight
is awesome
Studytonight
is awesome

On the other hand, passing the whitespace to the end parameter indicates that the end character has to be identified by whitespace and not a newline (which is the default).

print("Studytonight", end=' ')
print("is awesome")

Output:

Studytonight is awesome

The below example shows that any value can be passed to the end parameter and based on the content in the print statement, the end value gets displayed.

print("Hi", end=' Studytonight ')
print("you are awesome")

Output:

Hi Studytonight you are awesome

2. The sep parameter

Sometimes, we may wish to print multiple values in a Python program in a readable manner. This is when the argument sep comes to play. The arguments passed to the program can be separated by different values. The default value for sep is whitespace. The sep parameter is primarily used to format the strings that need to be printed on the console and add a separator between strings to be printed. This feature was newly introduced in Python 3.x version.

The below example shows that passing sep parameter as whitespace or not passing the sep at all doesn't make a difference. Execute every line of code to see the result.

print("Study", "tonight")
print("Study", "tonight", sep = ' ')

Output:

Study tonight
Study tonight

The below example shows different values that are passed to the sep parameter.

print("Study", "tonight", sep = '')
print("Study", "tonight", sep = ' & ')

Output:

Studytonight
Study & tonight

Note: The sep parameter, used in conjunction with the end parameter is generally used in production code to print data in a readable fashion.

print("Studytonight","has","been","created","for", sep = " _ ", end=" _STUDENTS")

Output:

Studytonight _ has _ been _ created _ for _STUDENTS

Conclusion:

In this post, we understood the significance of the 'sep' and 'end' parameters that are used in the print method.

You May Also Like

  • Checking whether two Lists are Circularly Identical using Python

  • The 'setdefault' Function in Python

  • The 'trunc' function in Python

  • Python *args and **kwargs Variables