Page 67 - Information_Practice_Fliipbook_Class11
P. 67
3.10.1 input()
The input() function reads the text entered by a user until a newline is encountered. Suppose we wish to fetch the
name of a programming language as an input from a user. For this purpose, the function input() may be invoked
as follows:
>>> language = input()
Python
On execution of the above statement, Python prompts the user for the name and waits for the input. When the user
enters the string, Python and hits the enter key, Python assigns the string Python to the variable language, as
verified below:
>>> language
'Python'
The above use of the input() function seems simple and devoid of any problems. If we write a small program that
needs only one or two inputs and execute it soon after we are done with the program, then this approach works fine.
But real-life programs may require several inputs and they may be executed days, months, or years after they are
developed. In such scenarios, it is nearly impossible to remember the inputs required by the program. In fact, most of
the software is used by people without having had a role in developing it. Therefore, it is good practice to display a
suitable message indicating the inputs required, as illustrated below:
>>> input('Enter name of a Programming language: ')
Enter name of a Programming language: Python
'Python'
>>> language = input('Enter name of a Programming language: ')
Enter name of a Programming language: Python
>>> language
'Python'
The following syntax describes the use of input()function to assign the user input to a variable.
<variable> = input([<message-to-prompt-user-to-enter-input>])
The input() function reads the text entered by a user until a newline is encountered. In the above syntax description,
input is the keyword and message-to-prompt-user-to-enter-input is the message that is displayed on
the screen, followed by the cursor. Note that message-to-prompt-user-to-enter-input is enclosed
within square brackets, indicating that its use is optional. Indeed, we have already seen an example of the use of
input() function where we did not specify any message-to-prompt-user-to-enter-input. In the above
description, variable is to be replaced with a variable name. The function input() accepts user input from the
keyboard. On seeing the cursor, the user enters the required inputs and hits the enter key to indicate the end of input.
Once the user presses the enter key, the input() function yields the user input, which is assigned to the variable
on the left-hand side of the assignment operator. Python considers user inputs as strings. The use of the input()
function is illustrated below:
>>> rollNo = input('Enter your roll number: ')
Enter your roll number: 12
>>> name = input('Enter your name : ')
Enter your name: Kabir
>>> print(rollNo, name)
12 Kabir
In the above example, the variable rollNo is assigned the value 12 entered by the user. Similarly, the variable
name is assigned the value Kabir. As mentioned above, Python interprets the user inputs as strings. Thus, each of
the values 12 and Kabir, entered (by the user) is an example of a string. Next, suppose we wish to calculate the
perimeter of a rectangle based on the input length and breadth entered by a user.
Basics of Python Programming 53

