Signup/Sign In
LAST UPDATED: MARCH 14, 2023

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

Technology

    We all have written multiple print statements in Python with or without parameters. But have you ever wondered about 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 is printed on the console.

    What are the Python sep and end Parameters in Python?

    The end parameter in Print is used to append any string at the end of the output of the print statement in python while the sep parameter is used to format the strings that need to be printed on the console and add a separator between strings.

    Let's learn more about these Python print parameters!

    sep end parameters in Python

    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 the method ends with a new line. 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 new line (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


    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 version.

    The below example shows that passing the 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 tutorial, We discussed the sep and end parameters in the print statements of Python then we discussed both of them in detail and executed examples of both. You can learn awesome stuff like this for Free at StudyTonight. We offer a beginner to advance level Python Course that can help you grow your Python skills in a very fun and interactive environment!

    You May Also Like

    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS