Lecture 3: Python Basics – User Input

So far, we’ve learned the basic data types of python by building a program that calculates the sum, difference, and average of two numbers. We’ve also printed our answers to the users with the following information. In this lecture, we’ll learn how to take user input and manipulate the data that we receive from the user instead of setting a number to a variable as we did in the previous lectures.

We can start by using the input() function provided by python to take input from a user, next we can print out whatever the user entered.

Let’s start by spinning up a Python REPL instance. and inserting the code below:

userInput = input("Type anything you want here: ")

print(f'Your input: {userInput}')

Let’s break this down into pieces we can digest and understand. First, we declare a variable called userInput that stores the value of anything that the user enters, we then use the input() function to read input from the user:

userInput = input("Type anything you want here: ")

Next, we print the information taken in from the user with the print() function:

print(f'Your input: {userInput}')

If we run the console we’ll receive the following prompt

> Type anything you want here: 

Let’s enter some text like the one listed below (feel free to replace Adam with your name):

> Type anything you want here: Hello my name is Adam!

Click Enter and you should get the following below!

> Hello my name is Adam!

Congratulations on writing your first python program utilizing User Input!