Kahibaro
Discord Login Register

Getting input from the user

Reading Text from the Keyboard with `input()`

To get information from the user in Python, you normally use the built-in input() function. This lets your program pause, wait for the user to type something, and then continue.

At its simplest:

name = input("What is your name? ")
print("Hello,", name)

Here is what happens step by step:

  1. Python shows the text inside the quotes: What is your name?
  2. The program waits until the user types some text and presses Enter.
  3. Whatever the user typed is returned by input() and stored in the variable name.
  4. print() shows a message using that value.

The line you see on the screen (What is your name? ) is often called a prompt.

The `input()` Function Always Returns a String

No matter what the user types, input() gives it to you as a piece of text (a string, str), even if it looks like a number.

age = input("Enter your age: ")
print(age)
print(type(age))

If the user types 25, the value of age is "25" (a string), not the number $25$.

You will learn how to turn this text into numbers in a later subsection of this chapter (“Converting input to numbers”). For now, just remember:

Using a Prompt vs. No Prompt

You can call input() in two ways:

With a prompt

This is the most common and user-friendly way:

color = input("What is your favorite color? ")
print("You like", color)

Without a prompt

You can also call input() without giving any message:

text = input()
print("You typed:", text)

If you do this, nothing appears on the screen to tell the user what to do, so they might be confused. Usually you should provide a clear prompt.

Storing and Reusing User Input

The value from input() is usually stored in a variable so you can reuse it later.

city = input("Which city do you live in? ")
print("You live in", city)
print("Wow, I have always wanted to visit", city + "!")

Key ideas:

Multiple Inputs in One Program

You can ask for more than one piece of information by calling input() multiple times.

first_name = input("First name: ")
last_name = input("Last name: ")
print("Hello,", first_name, last_name)

Each input() call:

  1. Shows its own prompt.
  2. Waits for the user’s response.
  3. Returns a string that you can store in a separate variable.

Input Order and Program Flow

When Python reaches a line with input(), it stops and waits until the user responds. The lines after that do not run until the user has typed something and pressed Enter.

Example:

print("Program started")
answer = input("Press Enter to continue...")
print("You continued.")
print("Program finished")

The output flow:

  1. Program started appears.
  2. Program waits at input("Press Enter to continue...").
  3. After the user presses Enter, You continued. appears.
  4. Then Program finished appears.

This “pause and wait” behavior is important when writing interactive programs.

Stripping the Newline (What Enter Really Does)

When the user presses Enter, Python does not include that Enter key itself in the returned string. input() gives you just what the user typed before pressing Enter.

For example, if the user types:

You usually do not need to worry about Enter adding extra characters at the end; input() handles that for you.

Simple Text-Based Interaction Patterns

Putting input() and print() together lets you create simple text conversations.

Ask a question, then react

food = input("What is your favorite food? ")
print("Nice! I like", food, "too.")

Ask a yes/no question

Keep in mind the answer is still a string:

likes_python = input("Do you like Python? (yes/no) ")
print("You answered:", likes_python)

Later, with conditions, you will be able to react differently depending on whether the user typed "yes" or "no".

Naming Conventions for Input Variables

Although Python lets you choose almost any valid name, certain names make your code easier to understand:

Examples of clearer vs. less clear:

# Clear
email = input("Enter your email: ")
country = input("Country: ")
# Less clear
a = input("Enter your email: ")
b = input("Country: ")

Clear names will help you when your programs get longer.

Basic Input Validation (Very Simple Checks)

Full error handling and conditions are covered later, but you can already start doing very simple checks using what you know so far—mainly by just looking at what the user typed and making comments.

For example, checking if the user left something blank:

username = input("Choose a username: ")
print("You entered:", username)

Right now, this program will accept anything, including blank input (just pressing Enter). Handling incorrect or missing input in a smart way will be much easier once you learn about conditions and errors in later chapters. For this section, just recognize that input() itself does not check whether the answer “makes sense.” It simply returns whatever was typed.

Writing Small Programs That Ask for Input

Here are a couple of small, self-contained examples focused only on getting and showing user input as text.

Example: Greeting program

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

Example: Simple profile

name = input("Name: ")
hobby = input("Favorite hobby: ")
animal = input("Favorite animal: ")
print("Profile summary:")
print("Name:", name)
print("Hobby:", hobby)
print("Favorite animal:", animal)

These examples only treat what the user types as text. Later in this chapter, you will see how to convert text input into numbers so you can do calculations with it.

Views: 15

Comments

Please login to add a comment.

Don't have an account? Register now!