Quick Reach
The Print function
Python comes up with a print function to display the output. Before version 3.x of Python, the print was used as a statement i.e. it does not require brackets (). Whereas, in the latest version, Python uses print as a function while strings, variables etc has to be enclosed in brackets.
If the print statement is used in the latest version, an error will be generated.
A print example with code
In our examples of Python tutorial, we used the latest method of displaying the output, i.e. using python print as a function, as such it uses version 3.x.
Syntax of print()
Following is the syntax of print function:
Print(“string” or variable_name )
Example of using print function
Following example shows how to use the Python print function to display variable value. The first print function displays a string variable while other displays an int variable by using the print Python function:
1
2
3
4
5
6
7
8
9
|
print (“This is simple print statement”)
str_variable = “This will display a string variable”
print (str_variable)
int_str = 12345
print (“This is mixed string and int”, int_str)
|
The output will be:
This is simple print statement
This will display a string variable
This is mixed string and int 12345
Print function in a for loop
Similarly, you can use the print function in the for loop to display values during the iteration. In this example, we have used a List of Python with six elements. Then a for loop is used to iterate through the list. Inside the for loop, the print function of Python is used to display the List elements.
See the example by clicking the link below:
1
2
3
4
5
|
words = [‘This’,‘is’,‘Python’,‘For’, ‘Loop’,‘Example’]
for j in words:
print (j)
|
Output will be
This
is
Python
For
Loop
Example
Also see – Python User input | Python While loop
Leave A Comment?