Here we will learn Python’s most important function named print() which is used to print the specified message on the screen. In previous Python built-in functions tutorials, we have seen lots of functions along with examples.
Python print() function is the most useful function which is used to print some message or print the value of the variable on the screen.
Headings of Contents
Python print() function
Python print function in Python is a built-in function which means you don’t need to install it using the pip command or you don’t need to import it from any module or library. This function is used to print a specified message or print the value of the specified variable on the screen.
Syntax
The syntax of the print function in Python is:-
print(object(s), sep=separator, end=end, file=file, flush=flush)
print() Parameters
print function in Python support various parameters, which describe below.
- object(s):- Any object, and as many as you like.
- sep:- separator, Specify how to separate the objects. By default, an empty string (“”) is used as a separator.
- end:- Optional. Specify what to print at the end. By default, it is set to a new line ( \n ).
- file:- Optional, An object with a write method.
- flush:- Optional, A boolean. Specifying if the output is flushed (True) or buffered ( False ). The default is False.
Python print functions examples
Here we have explored the Python print function along with different-different parameters.
Example: Using print() function with a single parameter
# Print simple one object.
print('Welcome to Programming Funda')
#Print more than one object.
print('Welcome to', ' Programming ', 'Funda')
#print variable value.
a = 12
b = 23
print(f"The Addition of {a} and {b} is:- {a+b}")
Output
Welcome to Programming Funda
Welcome to Programming Funda
The addition of 12 and 23 is:- 35
Example: Using print() function with sep parameter
Using print function with a second parameter that is sep. sep parameter is used to separate the objects by character set to sep param, As you can see in the below example.
# print more than one object with sep parameter.
print('Welcome to', ' Programming ', 'Funda', sep='---')
Output
Welcome to--- Programming ---Funda
Example: Using print() function with end parameter
Using print function along with end parameter. end parameter is used to print a string to be the end of the string.
string = "Best Programming Tutorial is:- "
end_string = "Programming Funda"
print(string, end=end_string)
Output
Best Programming Tutorial is:- Programming Funda
As you can see in the above Output, How, end
parameter add the string to the end of the string.
Example: Read the content of the file
As we know, We can read the contents of the file using the print() function. Let’s see how can we do that. Of course, we will use Python open() function to open the file in read mode and after that, we will use the print() function to read the contents of the file.
We have a txt file demo that contains some data, Now we will use the open() and print() functions to read the content of the file.
demo.txt
Programming Funda is one of the best place to learn coding along with explanation and examples.
Code
# opening file in read mode
file = open("demo.txt", "r")
# read content of the file
print(file.read())
Outpu
Programming Funda is one of the best place to learn coding along with explanation and examples.
Conclusion
In this tutorial, you have learned all about Python print function along with examples. This is the most popular and usable function to print specified objects.
If you are a Python programmer, Then we don’t need to explain more because the print function is used day by day.
If you like this article, please share and keep visiting for further Python built-in functions tutorials.
Python built-in functions
For more information:- Click Here
Thanks for your valuable time… 🙏🙏❤️❤️