We use cookies to give you a better experience. Carry on browsing if you're happy with this, or read our cookies policy for more information.

Skip main navigation

Getting input from the user

In order to interact with the user, you can get your program to ask for some input. In this video, Martin O'Hanlon explains how.
3.9
So far, you’ve only used print to output some text. Next, you’re going to use input to prompt the user to enter some data. Now, input’s going to display a message on the screen. The program’s then going to wait for the user to give a response and press Enter. And not response then will be stored as a variable so you can use it in your program. Print and input are examples of functions. Functions are pre-made programs which are typically created to perform a single task. Now, Python has many in-built functions, allowing you to perform a large range of different tasks. Now, let’s add some code to the bottom of your program to use input to capture the user’s name.
38
I’m going to create my variable, called user’s name. I’m going to say it’s going to be equal to input. I’m going to open a bracket and I’m going to put the phrase that I want the user to see. Enter your name. Now, let’s just break down this line of code. Uses_name equals is going to create a variable. And that variable is going to be assigned the value that input gives me. Input is obviously in the name of the function. And between the brackets is the phrase that’s going to be given to the user. Now we’ve used input to capture the user’s name. Let’s just print it to the screen.
83.1
Now, let’s run our program and see what happens. So we’ve run our program. I get a prompt saying enter your name. I’m going to put Martin. And I’m going to press Enter. And it prints out my name because that’s what’s held in the users_names variable. But it’s not a very friendly message. Let’s improve that by adding to the front of it, hello, users_name. Welcome. And again, let’s run our program to see what happens.
115.3
Enter your name. Martin. Hello, Martin. Welcome. So why not grab a friend, run your program, and ask them to input their name? Have some fun with it.
So far you have only been using print to output text; your program currently lacks the ability to interact with the user.
In this section you will be using input to prompt the user to enter some data.
input will put a message on the screen, wait for the user to give a response and press Enter, and then store the response as text in a variable so you can use it in your program.
print and input are examples of functions. Functions are pre-made programs which are typically created to perform a single task. Python has many inbuilt functions allowing you to perform a large range of tasks.
  • Add the following code to the bottom of your program to use input to capture the user’s name and store it in a variable.
users_name = input("What is your name? ")
Breaking down this line of code you can see what each element does:
  • users_name = creates a variable called users_name and assigns it a value
  • input() uses the function input to display a prompt and capture the user’s response
  • "What is your name? " – the value between the () – is the prompt which will be output to the screen
Now the text the user entered is stored in an variable (users_name) you can use the data in your program.
  • Lets use print to output the user’s name.
print(users_name)
  • Improve the output by adding some text to the start and end of the message.
print("Hello " + users_name + ", welcome.")
  • Grab a friend, run your program and ask them to input their name.
Note: you should enter your name in the REPL after the “What is your name?” prompt and press Enter when you have done so.
This article is from the free online

Programming 101: An Introduction to Python for Educators

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education