Python Print

The print() function is actually the backbone of computer languages. This function helps to display the output or desired message on the user’s screen or wherever the user wants on this output device. This message returned from the print() function can either be a data type such as a string, or it can be an object or even an object which has undergone some conversions before coming out as the output. In this article, we will be discussing the importance of the print() function in python along with an example of writing the print() function in python. We will also discuss the python print() parameters, their uses, and some python code examples for better understanding. We will also talk about string literal and its examples.

Print Function In Python

As we have discussed the importance of the print() function in computer programming, it plays the same role in python as well. This function helps to print the desired output on the screen of the user or his device. The output can also be displayed in the text file. It is not always mandatory that arguments need to be passed in the print() function. The only requirement is that it needs to have an empty parenthesis whc=ich will execute the function at the end of the python program. The process will happen by calling the function by name.

Below is the basic python program using the print() function to display a message: 

message = 'Welcome to HKR training'

print(message)

Output: 

Welcome to HKR training

Python Print Syntax

print(*object(s), sep(separated)=' ', end=, file=sys.stdout, flush(Flush)=False)

Let us see the meaning of the parameters written in the syntax above:

Become a python Certified professional  by learning this HKR Python Training !

Python Training Certification

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

Python Print Parameters

Object

An object will be first converted to a string before giving an output message. The * before the object states that it consists of more than 1 value.

Sep(separator)

This is actually a separator. It comes into play to separate the objects when more than 1 object values are present in the code. The default value always remains ‘ ‘.

End

This is to print the end of the statement. This is not a mandatory field. The default value for the end is ‘\n’

File

This parameter states an object with the write method, actually string. If not specified, the default value which will be displayed on the screen is sys.stdout

Flush

The input-output python statements are generally used in bits or chunks. Here, the flush plays a major role in deciding whether the statement needs buffering or not. If the flush value is false, the stream is not flushed however, if the output value is true, the stream gets flushed forcefully. The default value for flush always remains false.

Now, let us understand the above parameters and have a look at some examples below:

Below is a python code to display the output using a print() function with multiple object values:

print("Welcome to HKR Training")

x = 5

print("The value of x =", x)

y = x

print('The value of x =', x, '= the value of y')

 Output: 

 Welcome to HKR Training

The value of x = 5

The value of x = 5 = the value of y

In the python code above,

  1. The sep i.e. separator is ‘ ‘. the main role of the separator is to create spaces between the 2 objects.
  2. We have used end parameter by using the \n (character for newline).
  3. The output will be printed on the screen using the file.
  4. As the stream is not flushed forcefully, therefore the flush value is false.

Below is a python example to demonstrate the print() function with more than one values:

print("Hello!", "Welcome to HKR Training")

Output: 

Hello! Welcome to HKR Training

>

Below is a python program to display a tuple using the print() function:

z = ("Red", "Blue", "Green")

print(z)

Output: 

('Red', 'Blue', 'Green')

>

Below is a python code to display the output with multiple values along with the separator:

print("Hello!", "Welcome to HKR Training", sep="---")

Output: 

Hello!---Welcome to HKR Training

>

Below is a python code to display the output using a print() function with a separator along with the end value:

z = 5

print("The value of z is =", z, sep='0000', end='\n\n')

print("The value of z is =", z, sep='0', end='')

Output: 

The value of z is =00005

 The value of z is =05

Below is a python code to display the file parameter:

source_File = open('print.txt', 'x')

print('NICE AND BEAUTIFUL FILE', file = source_File)

source_File.close()

Output

This program will try to open the file mentioned by the user on his device in writing mode. In case, there is no file as print.txt present, then it will create one file and open it in writing mode.  Finally, the file.close() method will close the file. 

Top 50 frequently asked Python interview Question and answers !

Subscribe to our youtube channel to get new updates..!

String Literals

In the print() statement in python, string literals are actually used for the formatting as well as designing of the specific string on how it will appear on the display screen when printed.

  • \n: This string literal is a newline character. The user can use this to add a new line before printing another statement.
  • “ ” : This is basically an empty quote. The user can use it to print an empty line.

Below is a python program to understand how string literals work using a print() function in python:

print("Hello! \nYou are welcome to HKR Training")

Output: 

Hello! 

You are welcome to HKR Training

If you want to Explore more about Python? then read our updated article - Python Tutorial.

Python Training Certification

Weekday / Weekend Batches

Conclusion

In this article, we have discussed the use of the python print() function in computer programming. The print() function is actually used to display the output or desired message on the user’s screen or wherever the user wants on this output device. This message returned from the print() function can either be a data type such as a string, or it can be an object. We have also discussed the print() parameters in python along with their examples and string literals. Hence, the main idea of this article is to understand the use of the print() function in python. 

FAQs’

How does the print() function work in python?

The print() function in python can take up multiple values and objects and can display them as a single statement on the output screen.

What does \n character for in python?

This is a newline character that shifts the statement into a new line.

What does \r character for in python?

This is a character that helps in shifting the cursor to the beginning of the python code.

How does the user print output in python?

A user can print the output in python using this syntax:

print(*object(s), sep(separated)=' ', end=, file=sys.stdout, flush(Flush)=False)

Related Articles

  1. Python Split Method
  2. Python Variables
  3. Python Web Scraping

Find our upcoming Python Training Certification Online Classes

  • Batch starts on 26th Sep 2023, Weekday batch

  • Batch starts on 30th Sep 2023, Weekend batch

  • Batch starts on 4th Oct 2023, Weekday batch

Global Promotional Image
 

Categories

Request for more information

Amani
Amani
Research Analyst
As a content writer at HKR trainings, I deliver content on various technologies. I hold my graduation degree in Information technology. I am passionate about helping people understand technology-related content through my easily digestible content. My writings include Data Science, Machine Learning, Artificial Intelligence, Python, Salesforce, Servicenow and etc.