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. ExampleIn 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") OutputRunning the above code gives us the following result − Welcome to Tutorialspoint Welcome to Tutorialspoint
Updated on 22-Jul-2020 08:30:56
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 In this post, we will discuss how ‘sep’ and ‘end’ parameters can be used to change the way in which the contents of the
1. The end parameterThe end parameter is used to append any string at the end of the output of the By default, the 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.
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).
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.
Output: Hi Studytonight you are awesome 2. The sep parameterSometimes, 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.
Output: Study tonight Study tonight The below example shows different values that are passed to the sep parameter.
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.
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 You May Also Like
|