This tutorial explains how to use the Python Print function with ample examples and use cases to print variables, a list, printing with and without a newline, etc.:
In Python, the print() function is used to get the output and debug the code. This function is used to display the specified message or value in the console. The message can be a string or any other object.
We can say that the print function is useless in programming, but it is actually the most widely used and powerful tool for debugging. Debugging refers to the act to find, remove and fix the errors and the mistakes within the code.
What You Will Learn:
- Python print() Function
- Python Print() Function Syntax/Format
- Python Print Examples
- Python Print Variables
- Print String In Python
- Print Without Newline
- Python Print With Newline
- Print List In Python
- Print Function Arguments
- How To Print Other Data Types In Python
- More Examples Of Print In Python
- Python Print To File
- Frequently Asked Questions
Python print() Function
If something is not right in the code, then we can use the print function to print what is happening in the code. Many times, we expect a certain value of a variable to be one thing, but we cannot see what our program sees.
If we use the print function to print out the value of a variable, then we will see what we thought was not present in our program.
Python Print() Function Syntax/Format
print( *object, sep= “ ”, end = “\n”, file= sys.stdout, flush= False )
- *object: One or more objects to be printed.
- sep: Separator between objects. Default value = single space
``` a = ‘Welcome’ b = ‘Python’ print(a, b, sep = ‘ , ‘) ```
- end : The value is printed after all the specified objects are printed. Default value = Newline
``` a = ‘Welcome’ b = ‘Python’ print(a, end = ‘ & ’) print(b) ```
“ Welcome & Python”
- file: Stream where the output is to be printed. Default value = Standard output
Create a file with the name “demo.py” and paste the following code:
``` newfile = open(‘ demo.txt ’, ‘ w ‘ ) print(‘ Welcome to the tutorial ’) newfile.close() ```
Run the program using “python demo.py > output.txt”. It will create a file “output.txt” and add the print text into it.
- flush: It is used to buffer and unbuffer the output. The default value is “False” i.e. the output is buffered. If we set the “flush = True” then, the output is unbuffered and its processing will be slow.
``` demo = open(“demo.txt”, “a”) demo.write(“Welcome!”) demo.flush() demo.write(“One more line!”) ```
Python Print Examples
print( ): This function is used to display the blank line.
print(“strings”): When the string is passed to the function, the string is displayed as it is.
Example: print( “ Hello World ” ), print ( ‘ Hello World ’) and print ( “ Hello ”, “ World ” )
We can use single quotes or double quotes, but make sure they are together.
Run the command “python” in the terminal, and it will open the Python console where you can check the output simultaneously!
Run the following statements and see the output to get to know how the print function works!
- “ print( “ Print_Function ” ) ”
- “ print( ‘ Print_Function ’ ) “
- “ print( “ Print”, “Function ” ) ”
As we are talking about the print() function, it would be interesting to understand concatenation. Concatenation means to combine things.
In the print() function we use the “ + ” or “ , ” symbol to combine the two or more strings or we can use “ \ ” backslash. This character is known as the escape character. It will escape the characteristics of the character.
Note: If we are using “ , ” to combine the strings then, there will be a space between the two strings. If we are using the “ + ” symbol then, there will be no space between the two words.
``` print( “ Welcome to the article! ”, “ Have a nice day! ” ) ```
``` print(“ Welcome to the article! ”+ “ Have a nice day! ” ) ```
``` print (“ Welcome to the article! ”) \ ```
Python Print Variables
Strings can be assigned to variables. For example, we have two strings named as “str1” and “str2”
``` str1 = ‘ Welcome ’ print(str1) ```
``` str1 = ‘ Welcome ’ str2 = ‘ Back ’ print(str1, str2) ```
Print String In Python
Print using as a string uses the “ %s ” character to refer to the variable as a string in Python.
``` str1 = ‘ Python ’ print(“Hey! %s” % str1) ```
Print Without Newline
In Python if we want to print the statement with no newline, then the syntax will be:
``` print( “ Hello ”, end= “” ) print( “ Guys! ” ) ```
Python Print With Newline
In Python if we want to print the statement with a newline then the syntax will be:
``` print( “ Hello! ” ) print( “ Guys! ” ) ```
Print List In Python
In Python, the list is the combination of duplicate values with their distinct positions. All the values present in the list can be passed in the sequence at the time of the creation of the list.
In this example the list contains the duplicate values.
``` demolist = [ 1, 1, 2, 2, 3, 4, 5, 6, 7, 8] print(“Output: ”) print(demolist) ```
Output: [ 1, 1, 2, 2, 3, 4, 5, 6, 7, 8]
Print Function Arguments
In Python, the arguments are the values that we passed in the function when it is called.
In the example “ x ” and “ y ” are the two arguments that we passed in the addition function.
``` def addition ( x, y ) print( x + y ) addition(7,8) ```
It will return the sum of two numbers that we passed as arguments.
How To Print Other Data Types In Python
- %d: is used for Integer.
``` print( “ Number: %d ”, % 10 ) ```
- %e: is used for Exponential.
``` print( “ Exponential Number: %e ”, % 10 ) ```
- %f: is used for Float.
``` print( “ Float Number: %f ”, % 10 ) ```
- %o: is used for Octal.
``` print( “ Octal Number: %o ”, % 10 ) ```
- %x: is used for Hexadecimal.
``` print(“ Hexadecimal Number: %x ”, % 10) ```
More Examples Of Print In Python
Given below are the various ways to use the print() function in Python:
“ \n ” is used for Line break. ``` print( “ one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten ” ) ```
If we want to write one-word multiple times without repeating.
``` print( ‘ -Hello ’*5 <numbers you want to print the word>) ```
\t ” flag is used when we want a tab space in the words,
``` print( “”” Names: \t1 Riya \t2 Komal “”” ) ```
Python Print To File
In Python, the print() function supports the “ file ” argument. It specifies or tells the program where the function should write in a given object. By default, it is sys.stdout.
There are two essential purposes:
#1) Print to STDERR
It will specify the file parameter as sys.stderr. It is mainly used while debugging small programs. For the large programs it will be advised to use the debugger.
``` import sys print( “ Welcome ”, file = sys.stderr ) ```
#2) Print to external file
- It will specify the file parameter with the name of the required file instead of the default value.
- If the file does not exist, a new file will be created with the same name.
- If we do not specify the file parameter while calling the print() command, then it will show the text in the terminal.
- If we use the open command, then it will load the file in the write mode. When we call the print() function, the text will be written into the file directly.
``` # ‘ w ’ flag is used to write to the file. demo = open( ‘ demo.txt ’, ‘w’ ) print( “ Welcome ” ) demo.close() ```
Frequently Asked Questions
Q#1) Difference between print in Python2 and Python3.
Answer: In Python2 “print” was a statement and it prints the output with space in between.
For example, if we do the following
``` print( “ car : ”, car ) ```
We give one argument and a tuple having two elements ( “ car: ” and the object car ). Tuple will print their representation that is mostly used for debugging purposes.
In Python3 “ print ” became a function and it needs parentheses.
For example, if we do the following:
``` print( 4, 6 ) ```
The output will be “ 4 6 ” and “ print 2, 3 ” will drop a syntax error as it is a function and needs the parentheses.
Q#2) How to port print from Python2 to Python3?
Answer: If we have a “ print ” statement in Python2 and want to port it into Python3 then, put the following in the top of the source file.
“ from __future__ import print_function”
Q#3) What does the print() function do in Python?
Answer: In Python, the print() function is used to show the message on the screen/console. The message can be a string or anything but it will be converted into a string before printing to the screen.
Q#4) What is %s %d in Python?
Answer: In Python “ %s “ and “ %d “is the string formats. Where “ %s “ is used for the strings and %d is used for the numbers.
Q#5) What does % mean in Python?
Answer: In Python, the “ % “ operator is called a Modulo operator and is used to print the remainder after dividing the numbers.
In this tutorial, we discussed the print() function and many other topics related to the print() function in Python.
To summarize, we covered:
- Introduction to the print() function in Python.
- Basic syntax of the print() function.
- Concatenation in print() function, how to join the multiple strings.
- How to print the variables, Strings, and other data types in the print() function in Python.
- How to print the without newline and with newline in Python.
- How to print the list in Python.
- How to print text into the file using the print() function.