Input and output pretty much define what a computer
does for you. In Python, the input() function and the
print() function are two essential components that
allow you to create interactive applications. In this
section you will learn how to leverage these powerful
functions in your applications.
Getting Input from the User
Python has the input() function to get information from
a user running your Python code. The user is asked a
question, and the program waits until the user types a
response. It really is as simple as that. The input()
function takes the characters that are entered and
automatically stores them as a string data type,
regardless of what the user enters. Here is an example:
Click here to view code image
>>> inpt = input('Type your name: ')
Type your name: Chris Jackson
>>> inpt
'Chris Jackson'
You assign a variable (in this case, inpt) to the input()
function with a text prompt so that the user knows what
is expected. That variable now holds the string the user
typed. What if you need to get an integer or a floating
point number? Since Python stores every input as a
string, you need to do a conversion on the data supplied.
Here is an example:
Click here to view code image
>>> inpt = float(input('What is the Temperature in
F: '))
What is the Temperature in F: 83.5
>>> inpt
83.5