Input function | BUS 104 – Python Exam

The input() function explained

What's your favorite color?

Someone asking another person what their favorite color is

In the real world, we verbally ask this question.

In Python, we'd utilize an input() function to ask the question.

The input() function enables us to ask the user for a value.

Variables explained

After asking someone for their favorite color, we'd remember their response mentally.

In Python, we need to remember this response with a variable.

Variables are utilized to remember values for later use in a program.

This is commonly used with input() functions to save the values that users input for later use in the program.

Coding an input() function

Let's write an input() function asking the user for their favorite color. Then, we'll utilize a variable to remember it and print it in the output.

Make sure to open a Trinket file to follow-along! (How to create a Trinket file?)

Let's check out this template for input() functions.


Since we're asking the user for their favorite color, we'll make our question ask this.

input("What is your favorite color?")

If we were to run this single line of code, the user's response would not be stored. We wouldn't be able to reference it later in the program.

Why? Because we haven't declared a variable to remember the user's input.

Declaring variables to remember

Let's declare a variable for the input() function that we have. We'll call it favorite_color...

favorite_color = input("What's your favorite color?")

Now, whatever the user inputs will be saved to the value of favorite_color.

You might be asking, why is it "favorite_color" and not "favorite color" or "Favorite Color"?


In Python, you must declare your variables without spaces and lowercased. Any spaces should be depicted with an underscore.

Although we're remembering the value of the user's favorite color from the input() into favorite_color, we're not doing anything meaningful with that variable yet.

Let's test that Python has actually remembered the value of favorite_color with a print() function!

favorite_color = input("What's your favorite color?")

Essentially, what we're doing here is printing the value of whatever the user enters in the input() in the output.

Type the above code in your new Trinket file. It should look like so:

Now, press the "Run" button. You should get the following output, depicted in the slideshow below:

  • User is asked for their favorite color.

Notice how after we press enter in the input(), the favorite_color variable's value successfully appears in the output!

Inputting integers and floats

The input() function automatically returns values as strings.

Therefore, when dealing with input() functions that should return integers, we should use the following template:


For floats, follow this template:


Why do we place "int()" and "float()" around the input() function?

Because it forces the input() to be returned as an integer or float rather than a string.

Whenever your input() needs to return an integer instead of a string, surround it with int()!
Whenever your input() needs to return a float instead of a string, surround it with float()!

Not doing this properly can result in annoying errors later on in your program.

Practice problem

Create a new Trinket file that asks the user the question, "What's your name?" with an input() function. Save this value as a variable. Then, after they type their answer and press enter, print their name in the output.

Check out the slideshow below to see how your output should look.

  • User is asked for their name.

HINT: You need to print the user's name. How do we print? Check the print() function template on the last page of your Python Cheat Sheet for some help!

name = input("What's your name?")

If you named your variable name anything else, that's okay. As long as you're output was the same, you're good to go!

Next up, we'll learn how to combine strings with variables to make dynamic messages with concatenation. Click below to continue!

Python Cram Kit

Want to unlock (PREVIEW ONLY) content? Get your Python Cram Kit now!

ToolsPython Nerd Notes (PREVIEW ONLY)
ConceptPrint function
ConceptInput function
ConceptConditionals (if, elif, else)
ConceptWhile loops (PREVIEW ONLY)
ConceptFor loops (PREVIEW ONLY)
ConceptData Types (PREVIEW ONLY)
ConceptNested Conditionals (PREVIEW ONLY)
ConceptFlag Variables (PREVIEW ONLY)

Leave a Comment