Signup/Sign In

The 'sep' and 'end' parameters in Python print statement

Posted in Programming   LAST UPDATED: AUGUST 3, 2021

    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("is awesome")
    print("Studytonight", end= "\n")
    print("is awesome")


    is awesome
    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")


    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")


    Hi Studytonight you are awesome

    Learn Python Language from Basic to Advanced Level

    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 = ' ')


    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 = ' & ')


    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")


    Studytonight _ has _ been _ created _ for _STUDENTS


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

    You May Also Like

    Published by: SmritiSatyan
    Tags:PythonPython print