Back to Course

Introduction to Python

0% Complete
0/0 Steps
  1. Python: Basics
    15 Quizzes
  2. Python: Control Flow
    12 Quizzes
  3. Python: Errors
    5 Quizzes
Lesson 1, Topic 14
In Progress

Python: User Input

Yasin Cakal 5 Oct 2021
Lesson Progress
0% Complete

Thus far, we’ve discussed how to assign values to variables in a Python file directly. However, we frequently require a user of a program to enter new data.

How are we to accomplish this? As it turns out, user input can also be used to assign a value to a variable.

While print() is used to print the value of a variable, input() is used to assign data to a variable. The input() function requires a prompt message to display to the user before allowing them to enter new data. For instance:

your_name = input("What is your name?")
print("Nice to meet you", your_name)

In this example, first, “What is your name?” is printed onto the screen. Then, you type your name and press ENTER. Next, python assigns the string you entered to the variable your_name. Let’s say you entered “Joe” as your name. Then you will have the following output:

What is your name?
Nice to meet you Joe

This is a relatively straightforward implementation of input(), but as you gain experience with Python, you’ll encounter an increasing number of engaging scenarios in which you’ll want to interact with your users.

%d bloggers like this: