‘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,
can we use “sep” parameter in loops..if yes can u text the syntax please
In case of loops you can use sep as mentioned below:
print(*[ for iter in range()], sep=”)
Syntax : print(*[ value_to_be_printed for iter in range()], sep=””)
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
Hi! What’s the difference between using commas and additions?
Using additions is slower because each addition builds a new string.
how can i put a sep with a specific number of spaces?
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.)
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
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.)
taking input from the user and printing it using sep.
Read the comment above, I thought I was replying to you.
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
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?
can i use both sep and end attributes in same print function