Python – Getting Input

python logo

Getting input into Python is relatively easy. Accepting inputs opens many opportunities for developing applications, even as a novice programmer. In this article, we show you how to accept input easily, store it into a variable, and print it back on the screen. Even the most complex apps can start by accepting some type of input. Imagine developing an application that allows users to search for houses to buy based on the town they input.

What You’ll Create

Today we will walk you through accepting a name and age and storing it in a dictionary. We are going to build off the last Python post, linked here, by using introducing what a dictionary variable is, and how it differs from a list, and we are going to throw you into the deep in by accessing the keys of dictionaries! Don’t worry, we brought rafts. Also, check out the additional resources section for links to official Python documentation.


A dictionary in Python is a data type that stores values as key pairs. Key pairs refer to items that are called or referenced by a key. Dictionaries do not allow direct access to values, so you’ll need to make keys memorable. Since we want to know a name and age, we will use the keys “name” and “age”. Please do not confuse dictionaries with tuples. A tuple is also a key-value pair, but they are immutable, meaning the values cannot be changed.

Getting Input into Python

We can use the built-in function of the input() and assign it to a variable to store what we need. Below is an example application that asks for a name and age and displays it on the screen.

print("Please enter your name: ")
print("Please enter your age: ")
print(f"Hello {name}. Your age is: {age}")

Copying and pasting the above code into your code editor and saving it to a .py file will allow you to run the code and see the results. The most important takeaway here is we assigned name and age to the input() function. The print function uses something called string formatting. We highly recommend using this method to print variables because it allows more control over data formatting. Just take note of the f that is prefixed before the output.

Storing data into a dictionary

A dictionary variable is identified by using {} assigned to a variable. In the example below, we will build off the previous example and store the name and age in a dictionary before displaying it. Here we go!

dPeople = {}
print("Please enter your name: ")
print("Please enter your age: ")
dPeople["name"] = name
dPeople["age"] = age
print(f"Hello {dPeople['name']}. Your age is: {dPeople['age']}")

The key takeaway is that dictionaries are defined with {} while assigning it to a variable. You get inputs using the built-in Python function of the input(). Finally, you assign values to dictionaries by calling the key-value and using the equals operator.

Additional Resources

Python IO Docs

Python Dictionaries

Leave a Comment