Printing to the Screen in Python



Printing to the Screen in Python

In Python, the print() function is used to display the data on the screen. The data can be a string or any other object. However, the resultant object will be converted into a string before printing it on the console or standard output device. In this article let's understand a brief about print() function with various operations.

Syntax of print() Function

Following is the syntax of the print() function in Python ?

print(value(s), sep= ? ?, end = ?\n', file=file, flush=flush)

Following are the parameters of the print() function ?

  • value(s): We can pass as many values as we want and they are separated by commas.
  • sep(Optional): Specify how to separate the objects, if there is more than one. The default value is ' ?.
  • end(Optional) : Specify what to print at the end. The default value is ?\n'.
  • file(Optional) : An object with a write method. By Default value is set to sys.stdout.
  • flush(optional): It represents a Boolean value that specifies if the output is flushed or buffered. Default is buffered.

Example

In the following example we have created two variables integer and a string, we printed all the variables with the print() function in Python ?

Name = 'Neha'
Age = 22
print("Name :",Name)
print("Age :",Age )

Following is the output of the above code ?

Name: Neha
Age: 22

Printing Using the 'end' Parameter

The end parameter is used to specify the content that is to be printed at the end of the execution of the print() function. The default value is set to \n, which moves the curse to the next line after execution of the print() statement.

Example

Let's understand the usage of the end parameter in the print() function with the following example ?

print("Welcome to Tutorialspoint,",end="")
print(" which is a great learning platform.")

Following is the output of the above code ?

Welcome to Tutorialspoint, which is a great learning platform.

Printing Formatted String

In Python, the %(modulo) operator is used for string formatting. It allows us to create a formatted string by placing a format string on the left side of the operator and providing one or more values or variables on the right side. These values are substituted into the format string at the positions specified by the corresponding format specifiers.

Example

Here, is an example of the string formatting in print() function ?

Name = "Ravi"
Roll_no = 25
Marks = 89.7
print("%s with %d scored %0.2f marks in exam" %(Name,Roll_no,Marks))

Following is the output of the above code ?

Ravi with 25 scored 89.70 marks in exam

Printing Escape Characters

When we use escape characters in the print() statement, they perform specific functions (such as \n for a newline or \t for a tab). To prevent the escape characters from being processed and print them as they are, we use a raw string. This is done by placing an r or R in front of the string.

Example

Following is an example of escape characters in the print() function ?

#without rawstring
print("Welcome to \n Tutorialspoint")
#with raw string
print(r"Welcome to \n Tutorialspoint")

Following is the output of the above code ?

Welcome to 
 Tutorialspoint
Welcome to \n Tutorialspoint
Updated on: 2024-12-12T19:32:44+05:30

607 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements