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.
>>> namer = input(“Please enter your name.”)
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.
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.
>>> age_r = input(“What’s your age?”)
- 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.
Country Life by John Sloane