How do Python end and sep parameters different in print ()?
In this article, we’ll look at the Python end and sep parameters in the print function. To see how they can be used to customize the output of the print()
function.
Introduction
In Python, the print()
function is a handy tool for printing output to the console. One of the most useful features of the print()
function is the ability to specify the end
and sep
parameters. Which eventually, gives control to format the output according to your choice.
What is the Python end parameter in print()?
The end
parameter allows you to specify a string that will be printed after the output of the print()
function. However, the end
parameter is by default set to a new line character. which means that each call to print()
will print its output on a new line. However, you can customize the end
parameter to specify a different character or string that you want to print at the end of the output.
Here’s the syntax for using the end
:
print(end=string)
Code language: Python (python)
An example of the use of the end
parameter to specify a string to be printed after the output:
print("Namaste", end=", ")
print("India")
Code language: Python (python)
Output
Namaste, India
Code language: Python (python)
This code will print “Namaste, India” on the same line, with a comma separating the two words, as shown in the code.
What is the Python sep parameter in print()?
The sep
parameter allows you to specify a string that will be used to separate the items being printed by the print()
function. By default, the sep
parameter is set to a single space character. which means that the items being printed will be separated by a single space. However, you can also customize the sep
parameter.
Here’s the syntax for using the sep
parameter:
print(sep=string)
Code language: Python (python)
Let’s see an example of the use of the sep
parameter to specify a different string to be used as a separator:
print("Namaste", "India", sep=", ")
Code language: Python (python)
Output
Namaste, India
Code language: Python (python)
This code will print “Namaste, India” on the same line, with a comma separating the two words.
What is the difference between sep and end in print()?
Python end and sep parameters in the print function mainly differ due to their location. means where they are used in the function. The end
parameter is used to specify a string that will be printed after the output. However, the sep
parameter is used as a separator between the items that you want to print.
Parameter | Description |
sep | Specifies the string to use as a separator between the objects being printed. The default value is ' ' . |
end | It specifies the string to use as a terminator after the objects are printed. The default value is '\n' . |
The sep
and end parameters in the print()
function specifies the separator between the arguments. By default, the separator is a space character. For example, the following code will print “Namaste India” with a space between the two words:
print("Namaste", "India")
Code language: Python (python)
Output
Namaste India
Code language: Python (python)
You can change the separator by specifying the Python end and sep parameters.
For example, consider the following code:
print("Namaste", "India", sep=", ")
Code language: Python (python)
Output
Namaste, India
Code language: Python (python)
This code will print “Namaste, India” on the same line, with a comma separating the two words.
Now consider the following code:
print("Namaste", end=", ")
print("India")
Code language: Python (python)
Output
Namaste, India
Code language: Python (python)
This code will also print “Namaste, India” on the same line, but with a comma separating the two words.
In both cases, we are printing “Namaste” and “India” on the same line. but the sep
parameter specifies a comma as the separator between the two words. while the end
parameter specifies a comma as the string to be printed after the words.
How to use end and sep parameters in Python’s print() function
Let’s move to see how you can use the Python end and sep parameters in the print()
function:
Using the sep parameter:
# Using a comma as the separator
print("Welcome", "to", "Codedamn", sep=", ")
# Using a tab character as the separator
print("Welcome", "to", "Codedamn", sep="\t")
# Using a custom string as the separator
print("Welcome", "to", "Codedamn", sep=" - ")
Code language: Python (python)
Output
Welcome, to, Codedamn
Welcome to Codedamn
Welcome - to - Codedamn
Code language: Python (python)
Using the end parameter:
# Using a symbol as the end character
print("Welcome", "to", "Codedamn", end="@")
# Using a custom string as the end character
print("Welcome", "to", "Codedamn", end="***")
# Using no end character
print("Welcome", "to", "Codedamn", end="")
Code language: Python (python)
Output
Welcome to Codedamn@Welcome to Codedamn***Welcome to Codedamn
Code language: Python (python)
Now, using both end and sep parameters:
# Using a comma as the separator and a space as the end character
print("Hello", "John", sep=", ", end=" ")
# Using a tab character as the separator and a custom string as the end character
print("Hello", "Jack", sep="\t", end="***")
# Using a custom string as the separator and no end character
print("Hello", "Jill", sep=" - ", end="")
Code language: Python (python)
Output
Hello, John Hello Jack***Hello - Jill
Code language: Python (python)
Conclusion
In summary, You can use sep in the print()
function to specify the separator between the string. while the end
feature to specify the string that is printed at the end of the line. These parameters can be useful for controlling the formatting of the output in your Python programs.
Frequently Asked Questions
What is the difference between SEP and end in print()?
The sep parameter in the print()
function specifies the separator between the arguments. Instead, You can use the end parameter to specify a character or a string that will be printed at the end of the line.
What is the use of the end parameter in print() used in Python?
The end
parameter is used to add a new line character if nothing is specified.
How does print() different from the input() in Python?
In Python, print()
is a function that outputs a string, or other data, to the console or an output stream. It takes one or more arguments, which can be variables, expressions, or literal values. If you have not specified any parameters. Then by default, the print()
function adds a newline character at the end of the output string.
The input()
function, on the other hand, reads a line of text from the console or an input stream and returns it as a string. It can also prompt the user to enter some input by displaying a message.
print("Hi, John!")
print(1 + 2)
Inputname = input("What is your name? ")
print("Hello, " + Inputname + "!")
Code language: PHP (php)
Output
Hi, John!
3
What is your name? Codedamn
Hello, Codedamn !
Code language: Python (python)
Although, the input()
function waits for the user to enter some text and press the Enter key. It then reads the text and returns it as a string. However, you can use the input()
function to get input from the user and store it in a variable for later use.
So, print()
is used to output data to the console or an output stream, while input()
is used to read input from the console or an input stream.
What is SEP in print Python?
The sep
parameter in the print()
function specifies the separator between the arguments,
What does the end do in the print() statement in python?
You can use the end parameter to specify the string that will be printed at the end of the line. By default, the end is a new line character.
Sharing is caring
Did you like what Srishti Kumari wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses:
304 students learning
Haris
Python Crash Course for Beginners
Surendra varma Pericherla
Learn Data Structures Using Python