Lesson 13: 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

>>> namer = input(“Please enter your name.”)
>>> print(namer)

“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.

Tips

1- In summary, what you type inside input() is shown to the user. What they type back as a response is assigned to the value of your input function.

2- String type should be remembered. Your message inside input() function should be in quotes because it’s a string. Also what your user types is assigned to your input() function so your input() function’s value also is a string. This is true even if your user enters a number.

Let’s see an example.

Example 2

>>> 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 str.

Next Lesson:

range() function

Country life painted by John Sloane2


John Sloane

Country Life

Country Life by John Sloane

“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