Use Input and Output Functions - Python Programming

This article will help you to understand about basic input and output functions available in Python. So that a program can get an input from the user and perform some actions. Similarly, output function will be used to print or display on the screen as a output.

In the previous articles, we have explained you the below topics that will help you to understand this articles much better from basics.


Use Input and Output Functions - Python Programming


There are two built-in functions widely used for standard input and output operations in Python,
1. Output function - print()
2. Input function - input() and raw_input()

Python Tutorials - Input and Output Functions


Lets see an example by writing a simple program.

Also Watch this "Input and Output functions" Tutorial video free on our YouTube Channel.

Output Function - print ()
Print function is used to print or display on screen as a output.

Syntax to use this print function is,

print ('message')

Where 'print" is the function to print the message enclosed within quotes and brackets.

Example:
[root@server1 ~]# python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print ("Welcome to Learntiguide.net Technical Blog")
Welcome to Learntiguide.net Technical Blog
>>>

Any data within single quote or double quote will be considered as a string and printed as it is. Hence the above will be printed as it is.

Declare and Print a Variable:
Lets see a simple program which print the value assigned to a variable.

[root@server1 ~]# cat myprog.py
name = "LearnITGuide.net"
print (name)

[root@server1 ~]#


In the above program "myprog.py", we have declared a variable "name" with the value "LearnITGuide.net" and used the print function to print the value of variable "name".


Here, We didnt specify any quotes within brackets. Hence this is not string and print function will check for the value of a variable "name" in the program it will print the value of the variable, If it didnt find the variable value, then you will get an error as "name" is not defined.

Output:
[root@server1 ~]# python myprog.py
LearnITGuide.net
[root@server1 ~]#

Multiple String Declaration:
You can also use multiple strings within print function separated by commas when need. Lets have a look on below example where used old and new print function.

[root@server1 ~]# cat myprog.py
print ("Welcome to Learntiguide.net Technical Blog")
print ("Welcome","to","Learntiguide.net","Technical","Blog")
[root@server1 ~]# python myprog.py
Welcome to Learntiguide.net Technical Blog
('Welcome', 'to', 'Learntiguide.net', 'Technical', 'Blog')

First line printed the value as expected, but the second line printed with some brackets. Because, Second print function syntax is not compatible with current interpreter, so you can use new feature like this at the beginning of the line.

from __future__ import print_function
__future__ is a pseudo-module which programmers can use to enable new language features which are not compatible with the current interpreter.
[root@server1 ~]# cat myprog.py
from __future__ import print_function
print ("Welcome to Learntiguide.net Technical Blog")
print ("Welcome","to","Learntiguide.net","Technical","Blog")

Re-run the program to see the difference.
[root@server1 ~]# python myprog.py
Welcome to Learntiguide.net Technical Blog
Welcome to Learntiguide.net Technical Blog

Both are similar. Its upto us to decide which one to use, but always use the enhanced feature.

Display the message along with variable value:
lets see how to print the message along with variable value.

[root@server1 ~]# cat myprog.py
from __future__ import print_function
name = "Bob"
print ("Hello", name, "Welcome to LearnITGuide Tutorials")

[root@server1 ~]#

In the above program, we have declared a variable "name" with value "Bob" and used the print function to display the value of a variable "name" in the display as "Hello Bob, Welcome to LearnITGuide Tutorials".
[root@server1 ~]# python myprog.py
Hello Bob, Welcome to LearnITGuide Tutorials
[root@server1 ~]#

Input Functions - input() and raw_input()
So far, we have seen a program with assigned value for a variable within the program as below.

from __future__ import print_function
x = "learnitguide"
print (x)

On some cases, we may wanted to take the input from user.

We have two basic functions which are input and raw_input, earlier we used input() in the older python, now we have raw_input().

syntax to use these function is,
raw_input('Display message')
input('Display message')

where "Display message" is the message we wish to display on the screen to get the user input.

Lets see an example,
[root@server1 ~]# cat myprog.py
from __future__ import print_function
raw_input('Enter your name : ')
input('Enter your Age : ')

Output:
[root@server1 ~]# python myprog.py
Enter your name : Bob
Enter your Age : 30

[root@server1 ~]#


Above program not displayed the value, we have not used any print function. If you wanted to print the value of the user input, Declare a variable and get an input from the user, also use the print function as below.

[root@server1 ~]# cat myprog.py
from __future__ import print_function
name = raw_input('Enter your name : ')
age = input('Enter your Age : ')
print ("Welcome",name,"your age is ",age)

Output:
[root@server1 ~]# python myprog.py
Enter your  name : Henry
Enter your Age : 32
Welcome Henry

lets see one more example for some basic calculation by getting an input from user for two variables and print the value after calculation.
[root@server1 ~]# cat myprog.py
from __future__ import print_function
x = raw_input('Enter your first value : ')
y = raw_input('Enter your second value : ')
z = x * y
print ("The final value is : ",z)

Where, "x" and "y" are variables to assign values entered by the user and it perform the multiplication. Again assign the value to variable "z" and print the final value.

Note : With raw_input function, value will be taken as a string, strings cannot be used for mathematic operations. so we need to convert it using int function. or we can use the input function which accepts the input as integers.

Option 1 : using int function to convert the string to numbers.
[root@server1 ~]# cat myprog.py
from __future__ import print_function
x = int(raw_input('Enter your first value : '))
y = int(raw_input('Enter your second value : '))
z = x * y
print ("The final value is : ",z)

Option 2 : using input function rather than using raw_input function.
[root@server1 ~]# cat myprog.py
from __future__ import print_function
x = input('Enter your first value : '))
y = input('Enter your second value : ')
z = x * y
print ("The final value is : ",z)

Input function only takes as a numbers.


Output:

Enter your first value : 10
Enter your second value : 20
The final value is : 200

Hope you have got an idea how to use the basic input and output functions in python, write some basic program and practice by yourself. Going forward we will play with more programs.
Support Us: Share with your friends and groups.

Stay connected with us on social networking sites, Thank you.

Post a Comment

0 Comments