Taking input in Python

269

To interact with users, it is necessary to take input from the user. So, in this article, we will learn about how to take input from the users in python so that we can design a user-interactive program. There are two inbuilt functions available in python to take input from a user from the keyboard, which are –

  • Input(prompt)
  • Raw_input(prompt)

Taking input from the user using Input(prompt)

This function takes the input from the user and then, converts it to a string. It returns an object of type ‘str’.

When the input function is called, the program is stopped and waits for the user input. When the user gives the input and after that, presses ‘enter’, the program resumes and returns what the user typed.

We will understand it better using the program given below –

name = input(“Please enter your name:”)
Print(“Welcome” + name)

Output :

Please enter your name: Harshit
Welcome Harshit.

Explanation –

When the input function is called, the string that is inside the input function as an argument is printed. In this case, ‘Please enter your name’ will be printed.

After that, when the user inputs the name, here ‘Harshit’ is given by the user as a name input. So, after that when the ‘Enter’ Key is pressed, the input is assigned to the variable ‘name’ which is created here to store the string. And further, the execution of the next line will take place and Welcome Harshit will be printed on the output screen.

Taking input from the user using raw_input(prompt)

Apart from this, Python provides a different way to take input from the user which is used of the raw_input() function. This function works in older versions of Python such as Python 2.x. This function takes exactly what is typed, then converts it to the string, and finally returns it to the variable that the user has declared to store it.

We will understand it better with the example given below –

name = raw_input(“Enter your name”)
print name

Output –

Enter your name – Harshit
Harshit

Here, ‘name’ is a variable that will get the string typed by the user. After that, as soon as the ‘enter’ key is pressed by the user, the raw_input() function is terminated. This function can also be used to take numeric data. But, in that case, there will be a need for typecasting.

Apart from these two methods, we can also use a third method to take input from the user which is by taking the input from the console.

Console – It is a computer terminal where a user may give inputs and the outputs are shown if the program is error-free or otherwise, the errors are displayed. The console screen in python looks like this –

Input is taken from the user by using the inbuilt function ‘input()’. This also takes the data from the user in string format. Further, if we want to take other integers or floats as inputs, we can use them too by typecasting, which we will learn in further articles.

We will understand it better by the program given below –

n = input()
print(“The input was - ”, n)

Explanation –

In this code, the user will enter the data, and the data will be stored to the variable n. And after the user presses the ‘Enter’ key, the next line runs which prints the data that the user has input.

Apart from this, we can also take multiple inputs from the user using the split() function.

The syntax of the split function is as below –

input().split(separator, maxsplit)
x, y = input("Enter two values: ").split()
print("Number of boys: ", x)
print("Number of girls: ", y)
print()

The output of the above code is –

Enter two values: 10 20 (These are the sample values that are taken here, values are given by the user)
Number of boys: 10
Number of girls: 20

So, this was an article about multiple ways in which we can take input from the user in python so that we can interact with users and make an interactive program.

Attempt Free Python MCQ Test

Previous QuizStatic Members in Java class with example
Next QuizBasics Programming Model and Application Instruction set By Dr. Omkar Pattnaik, Sandip University

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.