Lesson 13: Python input() function

input() function is very simple and straightforward. It allows getting user’s input in string format. This is how it works:

1-) Type your message inside input(), this will be displayed to the user. 

2-) And user will be shown a message or asked a question accordingly. 

3-) After that, whatever they enter will be the value of your variable.

Let’s see some examples:

Function 1: input()

input() function lets you get input from the user of your program.

Used Where?

When user(s) input is needed. The reason can be acquiring information, selection, comment, answer etc.

Syntax do(s)

1) Enter the message or question you want to show your user inside the input() function's parenthesis.

Syntax don't(s)

1) Don't forget to type your message inside quotation marks inside the input() function.
i.e.: input("message here")

Example 1: Simple input example

name = input("Please enter your name.")
print(name)

>>> Please enter your name… (Joe)
Joe

1) After the first line is executed, the user will see the message: “Please enter your name.” and they will have a chance to type on the screen.

2) After they hit enter, whatever they typed as a response to your message gets assigned to the namer variable we crated above.

3)When the second line is executed, print function prints out namer’s value (user’s input) as a string.

  • User will get a message saying: What’s your age?
  • Let’s say they enter 20 as an answer.
  • This will be assigned to the input() function’s value and variable age_r.
  • Then on the last line we are printing the type of age_r.
  • Even though user entered an integer (20), input function returns a string value: “20” and age_r type is printed as class ‘str’.

Tips

1- In summary, what you type inside input() is the message shown to the user. And what value gets assigned to the input object is the input user provides as a response to the message they see.

2- Input returns a string.

Example 2: What type of data does input function return?

  • Input function always returns string type regardless of the input. Check out this example:
age_r = input("What's your age?")
print(age_r)
print(type(age_r))

20
<class ‘str’>

  • User will get a message saying: What’s your age?
  • Let’s say they enter 20 as an answer.
  • This will be assigned to the input() function’s value and variable age_r.
  • Then on the last line we are printing the type of age_r.
  • Even though user entered an integer (20), input function returns a string value: “20” and age_r type is printed as class ‘str’.

Example 3: Input program with dictionary

  • You can pass any function that makes sense to your map() function, it doesn’t have to be in the form of . Here is an example where function is defined conventionally and then passed in map() function as an argument.
### Birthday Checker
birthday_list={'mom': '1/5/1862', 'dad': '5/20/1860',
'son': '8/12/1930', 'sister': '10/15/1885'}

answer=input("Family member please")

if answer in birthday_list:
    print("{}'s birthday is {}".format(answer.capitalize(), birthday_list[answer]))
else: 
    print("No record")

>>> Family member please …. (mom)

Mom’s birthday is 1/5/1862

  • In this little example yet again we see so many programming concepts working in harmony. Specifically for Python we see Python dictionary, conditional statement, input function, format method and Python strings’ capitalize method.
  • This program has a risk of not satisfying case sensitivity. What if some users enter MOM instead?
    • You could fix this in multiple ways, first, you could specify that answer needs to be in lower case in your message.
    • Another way, and this is more elegant for sure is making use of lower() method of strings in Python. Could you figure out where it should be placed?
      • in conditional statement line you can simple amend as answer.lower() and program will search lower case version of the input inside the birthday list.

Input function potential use cases

It’s just a little example to demonstrate what can possibly be achieved using Python’s input function. What other examples come to your mind? Here  are some ideas:

  • Search engine: In fact search engines we using many times everyday such as Google, Ecosia, Duck or Bing do something very similar to this. They take your input which creates a search query in their global servers and returns a search result.
  • Database search: You could also do it for a database. Get the input and program will search the database. Of course, getting the input is the easy part, search algorithm, maybe a smart search function with Artificial Intelligence and suitable computer program for it would be the real work.
  • Command execution: You could also execute specific commands based on the inputs. Imagine something like, turn on, turn off, turn up, turn down or close, open. You could even create your own program which opens the garage door when you type open. Although an on/off button would likely be more interesting. If you are interested in eventually developing such software you can check out this gui tutorial series with Python for ideas.
  • You could also treat user inputs a little differently and collect information. You could ask users to enter their ID number or business ideas and make a collection of list or dictionaries.
  • You could implement successive input functions for multiple usage of input.

So many ideas come to mind with this sympathetic useful Python function.

Next lesson will be another useful Python function range which is used to create ranges of numbers.

Next Lesson: range() function

“Have you installed Python yet?”
Having Python setup on your computer can expedite the learning experience and help you gain the real life skills in a non-simulation environment. Installing Python can have different nuances that can be confusing for everyone. We compiled a complete tutorial about the best and most convenient Python installing practices Check it out by clicking the button below.
SANDRA FILES