Getting input from users in Python (2/100 Days of Python)

Martin Mirakyan
4 min readJan 3, 2023

--

Day 2 of the “100 Days of Python” blog post series covering the input() function

In some cases, we might want to get input from a user in Python. That will allow the user to customize the behavior of the program. For example, if the program is a simple calculator, the user might want to specify which operation they would like to perform (addition, subtraction, etc.) as well as the values to be used in the calculation.

In other cases, we might get input from a user using our program, to allow the program to process data that is not known at the time the program is written. For instance, a program that analyzes user behavior on a website might ask the user to enter the URL of the website they want to analyze in order to retrieve the necessary data. Finally, getting input from a user can also be a useful way to debug a program, as it allows the developer to test different inputs and see how the program responds.

The input() function in Python allows you to read user input from the command line. To read input from the user, you can call the input() function and store the result in a variable:

name = input()
print('Hello,', name, '!')

This program will wait for the user to type some input, then press Enter. The input that the user types is then stored in the name variable. The program then will print the value entered by the user. In case the user enters Anna, the program will print Hello, Anna !.

Demonstration of the input() function in action with different user inputs

In Python, a variable is a name that refers to a value. When you create a variable, you specify the name you want to use, and then you can use that name to refer to the value stored in the variable. In our example, we created a variable name and then stored the value inputted by the user in the variable called name. You might create a variable called x and assign it the value 5. Then, you can use the name x to refer to the value 5 in your program. Variables are a crucial concept in programming, as they allow you to store and manipulate data in your code. They can be used to store all sorts of values, including numbers, strings (text), etc. We will talk about them in much more detail later.

Another example of using the input() function can be to ask the user for the organization name and their website to later analyze it:

organization = input()
url = input()

print('Analyzing the url', url, 'for organization', organization)

Prompting the user

In some cases, there might be too many inputs that the user should input when using the program. In those cases, the users can get confused and not remember the order of the data they need to input. We can prompt them to input the right data:

print('Enter your organization name', end=': ')
organization = input()
print('Enter the website URL', end=': ')
url = input()

print('Analyzing the url', url, 'for organization', organization)

In this case, the program will first print the prompt and then wait for the user to input the required information. Yet, it’s possible to achieve the same outcome with fewer lines of code, using the prompt of the input:

organization = input('Enter your organization name:')
url = input('Enter the website URL:')

print('Analyzing the url', url, 'for organization', organization)

This will display the prompt Enter your organization name: and wait for the user to type some input, then press Enter. The input that the user types is then stored in the organizaiton variable. The program then will print Enter the website URL: and wait for the user to type something and press Enter. That value will be stored in the variable url which then will be printed in the final line of the program.

Note that the prompt should always be a string (should be enclosed in quotation marks).

Demonstration of the input() function with different prompts

What’s next?

--

--