‘sep’ and ‘end’ parameter in print() function Python

In this tutorial, we are going to learn about the print function parameters i.e sep and end. Earlier we don’t actually have the print as a function but as a statement. So it becomes difficult to format and print the text and echo on the screen.
But now in Python 3.x print comes out to be a function that has the following parameters that needs to pass to it while calling it as it is a function. Print function acts as a standard output device in Python Version 3.x

Parameters inside the Print function i.e. Sep and End

print ( <variable name or string> , sep = <separator value> , end = <end value >)

Sep Parameter in Python

As the name suggests, sep is used for adding separators into the string provided. Basically, it acts as a simple tool to modify the strings by replacing all spaces inside strings with the specified separator as an input value to the “sep” parameter. If we don’t specify any value to the “sep” parameter the by default it is considered to be a no-space.

#code for using the default parameter
print('C','O','D','E', sep='') 
  
#for formatting today's date using backslash
print('19','02','2019', sep='/') 
  
#formatting using @ symbol
print('pavitra','codespeedy', sep='@')
Output:
CODE
19/02/2019
[email protected]

End Parameter in Python

End, as the name suggests, is used for ending pattern of the string supplied during input . Basically, it acts as a simple tool to modify the strings by formatting the strings in a specified format by the help of specified parameter as an input value to the “end” parameter. If we don’t specify any value to the “end” parameter the by default it is considered to be a new line character i.e. “\n”.

Earlier in python 2.x we used to supply a comma to the print statement to keep the output in the same line. But here we will be supplying empty strings to end so that the text is echoed in the same line.

# ends the output with a <white space>
print("Welcome to" , end = ' ')  
print("Code Speedy") # no value of end is assigned and hence newline by default

# ends the output with '#' hash 
print("Pavitra" , end = '#')  
print("Code Speedy")
Output:
Welcome to Code Speedy
Pavitra#Code Speedy

Also learn,

15 responses to “‘sep’ and ‘end’ parameter in print() function Python”

  1. Meghana says:

    can we use “sep” parameter in loops..if yes can u text the syntax please

    • Pavitra Walia says:

      In case of loops you can use sep as mentioned below:
      print(*[ for iter in range()], sep=”)

    • Rafael González QuIroz says:

      I have a question;
      in the lines:
      for i in range(4):
      print(i,esc=”,”) #the output is 0,1,2,3,

      #how can i get output 0,1,2,3 (with out colon) ?, have i allways to add new lines or modify the written ones ?
      for i in range(3):
      print(i,esc=”,”) #the output is 0,1,2,
      print(i) #the output loooks 0,1,2,3

  2. Franisco says:

    Hi! What’s the difference between using commas and additions?

  3. Constanza says:

    how can i put a sep with a specific number of spaces?

    • Jacob Steele says:

      I believe what that means is that if you do not specify the end parameter, it will add the end parameter of new line.
      print(‘Hi’,end=”)
      print(‘ there’)
      output: Hi there (This example explicitly assigns end parameter)
      print(‘Hi’)
      print(‘ there’)
      output: Hi
      there (This example did not explicitly assign end parameter, so its default is newline.)

  4. Madde says:

    If we don’t specify any value to the “end” parameter the by default it is considered to be a new line character i.e. “\n”
    >>>> I think this is wrong. It aligns all print statements in the same line

    • Jacob Steele says:

      I believe what that means is that if you do not specify the end parameter, it will add the end parameter of new line. print(‘Hi’,end=”) print(‘ there’) output: Hi there (This example explicitly assigns end parameter) print(‘Hi’) print(‘ there’) output: Hi there (This example did not explicitly assign end parameter, so its default is newline.)

  5. kalyan roy says:

    taking input from the user and printing it using sep.

  6. Jacob Steele says:

    Read the comment above, I thought I was replying to you.

  7. Rafael González QuIroz says:

    I have a question;
    in the lines:
    for i in range(4):
    print(i,esc=”,”) #the output is 0,1,2,3,

    #how can i get output 0,1,2,3 (with out colon) ?, have i allways to add new lines or modify the written ones ?
    for i in range(3):
    print(i,esc=”,”) #the output is 0,1,2,
    print(i) #the output loooks 0,1,2,3

  8. Aubrey says:

    What if I want to add a second “sep” in my code?

    For example, in the following “print” statement, I want to separate the first string from the second string with an & symbol, but I want to separate the second string from the third string with an *.

    print(“Study time in the city”, “and in the country”, “but not on the lake”, sep=’ & ‘, sep=’ * ‘)
    My expected output would be:
    “Study time in the city & in the country * but not on the lake”

    The code above obviously does not work, is there any way to do this?

  9. vamsi says:

    can i use both sep and end attributes in same print function

Leave a Reply

Your email address will not be published. Required fields are marked *