You can learn about Introduction to Python Programming Programs with Outputs helped you to understand the language better.
Python Programming – Print Statement
print is a function that prints something on the screen. To print any message as it is on screen, just type print and that message or argument in (”) or (” “).
print (“Hello World”)
For example, in the above-mentioned program, “Hello World” is a string and print is a Python com¬mand to write a string in quotes on the console or terminal window.
- print( ) statement without any argument will simply jump to the next line.
Now, let us consider some more examples of how to use the print command. Type the following print statements and run the program to see the output:
- Python Programming – How To Start Python
- Python Programming – Creating a Simple “Hello World” Program
- Python Programming – Statements
Program to display string using the print command.
|print ( ” Hello, How are you? ” )
print ( ” I am learning Python language . ” )RUN
Hello, How are you?
I am learning Python language.
Let’s do something more advanced:
Program to display sum of two numbers using print command.
|x = 2
y = 3
print (“Sum of”,x,”and”,y,”is”,x+y)
The Sum of 2 and 3 is 5
Note that whatever you write in (” “) or (‘ ‘) will be printed as it is and is not evaluated. So, ‘Sum of’ gets printed as it is without quotes and then the value of ‘X’ gets printed (i.e., 2); then ‘and’ is printed as it is. Then the value of y is printed (i.e., 3) and at the end, x+y is calculated and printed (i.e., 5).
Multiple values can be displayed by the single print() function separated by comma. Python will automatically insert spaces between them. The fol-lowing example displays values of name and age variables using the single print() function.
>>> print(“Name, name, “Age:”,age)
Name: Aman Age: 19
By default, a single space (‘ ‘) acts as a separator between values. However, any other character can be used by providing a sep, short for separator
parameter to change that space to something else. For example, using sep=’:’ would separate the argu¬ments by a colon and sep=’##’ would separate the arguments by two hash signs. In the following example is used as a separator character.
The output of the print( ) function always ends with a newline character. The print( ) function has another optional parameter end, whose default value is “\n”. This value can be substituted by any other character such as a single space (‘ ‘) to display the output of the subsequent print( ) statement in the same line.
Write the following print statements and check the output:
|print (“You are Welcome!”)
print (“I am enjoying learning Python language.”)
|num1 = 10
num2 = 7
print (“Sum of”,numl,”and”,num2,”is”,numl+num2)
|n1 = 5
n2 = 3
print (“Product of”,n1,”and”,n2,”is”,n1*n2)
As print( ) is used to print output on the screen, to input data into a program, i.e., to get data from the user you use input(). This function reads a single line of text as a string. If the input function is called, the program flow will be stopped until the user has given input and pressed Enter key. The text of the optional parameter, i.e., the prompt, will be printed on the screen.
|person = input(‘Enter your name:’)||person = input(‘Enter your name:’)|
|print(‘Hello’, person, ‘!’)||print(‘Hello’, person, ‘!’, sep=”)|