2. DEFINITION
In Python, we use the input() function to take input from the user. Whatever you
enter as input, the input function converts it into a string. If you enter an integer value
still input() function converts it into a string.
There are two functions that can be used to read data or input from the user in
python: raw_input() and input(). The results can be stored into a variable. raw_input() -
It reads the input or command and returns a string. input() - Reads the input and returns
a python type like list, tuple, int, etc
3. raw_input() Function:
The raw_input() Function which is available in python2 is used to take the input
entered by the user.The raw_input() function explicitly converts the entered data into a
string and returns the value.The raw_input() can be used only in python2.
example:
name=raw_input(“Enter your name:”)
print(“Hi %s,Let us be friends!”%name);
output:
Enter your name: Jeyabharathi
Hi Jeyabharathi,Let us be friends!
4. input() Function
In Python,we use the input() Function to take input from the user.Whatever you
enter as input,the input function converts it into a string.If you enter an integer value
still input() function converts it into a string.
example:
value=input{“Enter the string:”}
print{f’you entered{value}’}
output:
Enter the string: Apple
you entered Apple
5. Difference between input() and raw_input() Function
raw_input function input() Function
• The return type of raw_input is always string The return type of input need not be string only.
• raw_input() function takes the point from the
user.
input() function take the user input.
• its Syntax:
raw_input(input)
its syntax:
input(prompt)
• It takes only one parameter that is the input. It takes only one parameter that us prompt.
• It is only introduced in python 2.0 version hang till
user inputs
It converts the input into a string by removing the
trailing newline blocks untikl input received
• “hello world” but string “hello world”
• foo in snake_case
6. To accept input from keyboard,Python provides the input() function. This function takes
a value from the keyboard and returns it as a string.For example..,
str=input()
priya
print(str)
OUTPUT: priya
it is a better idea to display a message to the user so that the user understands what to
enter.This can be done by writing a message inside the input() function as:
str=input(“Enter your name:”)
Enter your name: Priya
print(str)
OUTPUT: Priya
7. once the value comes into the variable ‘str’,it can be converted into ‘int’ or ‘float’ etc.
This is useful to accept numbers as:
str=input(‘Enter a number:’)
Enter a number: 123
x=int(str)
print(x)
OUTPUT: 125
We can use the int() function before the input() function to accept an integer from the
keyboard as:
x=int(input(“Enter a number:”)
Enter a number: 123
print(x)
OUTPUT: 123
8. Similarly, to accept a float value from the keyboard ,we can use the float() functionn
along with the innput() function as:
x=float(input(“Enter a number:”))
Enter a number: 12.143
print(x)
OUTPUT: 12.143
we will understand these concepts with the help of a python program. Let’s write a
program to accept a string and dispalyed it..,
example :
str=input(“Enter a string:”)
print(‘U entered:’,str)
10. we can understand that the input() function is accepting the character as a string
only. if we need only a character, then we should use the index, as: ch[0]. Here 0th
character is taken from the string.
example :
ch=input(“Enter a char”)
print(“U entered:”,+ch[0])
12. A python program to accept a float number from keyboard.
example :
x=input(“Enter a number:”)
print(‘U entered:’,x)
output:
13. From the above output,we can understand that the float values are displayed to an
accurancy of 15 digits after decimal point. In the next program,we will accept two
integer numbers and display them.
A program to accept two integer numbers from keyboard.
example :
x=int(input(“Enter first number:”))
y=int(input(“Enter second number:”))
print(‘u entered:’,x,y)
15. The two numbers in the output are displayed using a space. Suppose we want to display
these numbers using a comma as seperator, then we can use sep=’ ,’ in the print()
function as follows.,
In this case,the output will be..,
Observe the commas after “U entered:’ and after 12.A better way to display these
numbers seperating them using commas in this,
print(‘u entered:’, x , y, sep=’ , ’);
u entered: 12 , 2
print(‘U entered: %d, %d ‘ % (x,y))
16. A Python program to convert numbers from other systems into decimal number system.
example:
str=input(‘Enter hexadecimal number:’)
n=int(str, 16)
print(‘Hexadecimal to Decimal=’ , n);
str=input(‘Enter Octal number:’)
n=int(str, 8)
print(‘Octal to Decimal=’ , n);
str=input(‘Enter binary number:’)
n=int(str, 2)
print(‘Binary to Decimal=’ , n);
18. To accept more tha n one input in the same liine,we can use a for llop along with the
input() function in the following format,
In the previous statement, the input() function will display the message ‘Enter two
numbers:’ to the user. When the user enters two values, they are accepted as strings.
These strings are divided wherever a space is found by split() method. so, we get two
strings as elements of a list.These strings are read by for loop and converted into
integers by the int() function.These integers are finally stored into a and b.
The split() method by default splits the values where a space is found.Hence while
entering the numbers, the user should perate them using a space.The square brackets[ ]
around the total expression indicates that the input is accepted as elements of a list
a , b = [ int(x) for x in input (“Enter two numbers :”) , split () ]
19. COMMAND LINE ARGUMENTS :
We can design our programs in such a way that we can pass inputs t the program
when we run command. For example, we write a program by the name ‘sum.py’ that
takes numbers and sums them. We can supply the two numbers as input to the program
at the time of running the program at command prompt as,
Here,sum.py is our program name.While running this program,we are passing two
arguments 12 and 20 t the program,which are called command line arguments.so
command line arguments are the values to a python program at command prompt or
system prompt.Command line arguments are passed to the program from outside the
program.All the arguments should be entered from the keyboard seperating them by a
space.
c:> python sum.py 12 20
20. These arguments are stored by default in the form of strings in a list in the name ‘argv’
which is available in sys module. Since argv is a list that contains all values passed to
tha program,argv[0] represents the name of the program,argv[1] represents the first
value,argv[2] represents the second value and so on..,,
c:/> python sum.py 12 20
argv
argv[0] argv[1] argv[2]
command line args are stored as strings in argv list
‘sum.py’ 12 20
21. In command line arguments, they are various ways of dealing with these types of
arguments.The three most common are:
using sys.argv
using getopt module
using argparse module
Using sys.argv
The sys module provides funnctions and variables used to manipulate different
parts of the python runtime environment .This module provides access to some
variables used or maintained by the interpreter and to functions that innteract strongly
with the interpreter.
22. we want to find the number of command line arguments,we can use the len()
function as: len(argv).The following program reads the command line arguments
entered at command prompt and displayss them.
example:
import sys
n=len(sys.argv)
args=sys.argv
print(‘no. of comman line args=’,n)
print(‘The args are: ,args)
print(‘The args one by one:’)
for a in args:
print(a)
24. using getopt module
Python getopt module is similar to the getopt() function of C.Unlike sys module getopt
module extends the seperation of the input string by parameter validation.it allows both short,
and long options including a value assignment.However,this module requires the use of the
sys module to process input data properly.To use getopt module,it is required to remove the
first element from the list of command-line arguments.
example:
import sys
import getopt
def full_name():
first_name=None
last_name=None
argv=sys.argv[1:]
27. Using argparse module
Using argparse module is a better option than the above two options as it provides a
lot of options such as positional arguments,default value for arguments,help
message ,specifying data type of argument etc..,
example
import argparse
parser=argparse.ArgumentParser()
parser.add_argument("a")
args=parser.parse_args()