Page Contents (hide)
User interaction is very common in Python programs. We typically display information to the user in the terminal and extract data from the user via the keyboard. As in Java, the terminal represents standard output and the keyboard standard input.
Java version 1.5 introduced the Scanner
class for extracting data from standard input. It provided a number of methods for extracting different types of data. Consider the following Java code segment for extracting a string from the user
In python, there is a single function for extracting data from the user. The following Python code segment is equivalent to that shown above
The raw_input()
function waits for the user to enter a line of data at the keyboard, terminated with a carriage return. The data entered by the user is then returned as a string. In this example, if the user enters Smith at the keyboard, name
will refer to the string "Smith"
.
When extracting data from the user, we typically prompt the user with a message indicating what should be entered. Since this is so common, Python allows an argument to be passed to the raw_input()
function to be used as a prompt. We can rewrite the previous example as
When executed, the Python interpreter will display the string on a new line and then wait for the user to enter the data.
The raw_input()
function can only extract strings. But what if we need to extract an integer or real value? In that case, the numeric value will be stored as a string of digits and we must convert it to a numeric value using the appropriate type conversion function. In the following example, we extract a floating-point value from the user by passing the input string to the float
constructor which converts the numeric string representation to a real value
The corresponding Java code would be
If the user enters 3.92
at the prompt, variable userInput
will contain the string value "3.92"
.
Using the float()
constructor, Python will convert that string representation to the numeric value 3.92 which is then stored in variable gpa
The code segment above can be written as a single statement by nesting the two function calls
In Python, as you have seen in a number of examples, the print
statement is used to display information to the terminal. Note that print
is a statement in Python and not a function which means the use of ()
around the argument list is optional.
The print
statement can be used to display values of any of the primitive types. Internally, Python handles the translation from the primitive type to a string in order for the value to be printed.
To display or print multiple values, you can use multiple print
statements or separate each value with a comma. In the latter case, Python will display the values separated with spaces.
![]() | Note:
You can not concatenate non-string values with strings as can be done in Java. |
The default behavior of the print
statement is to produce a linefeed after the value(s) are printed. Thus, two successive print
statements will produce two lines of text. For example,
would produce the following output assuming avg
contains a value of 85.2
GRADE REPORT! Your average grade = 85.2
You can suppress the linefeed by ending the print
statement with a comma
In this case, the results are printed all on one line since the first print
statement ends with a comma
GRADE REPORT!Your average grade = 85.2
A sample program is provided which illustrates user interaction. Data is extracted from the user and then used to compute an employee’s take home pay. First, consider the Java version of the program
and compare it to the equivalent Python version
Java version 1.5 introduced the printf()
method for use with the System.out
object to produce formatted output. Python also supports formatted output using the special string formatting operator (%
) along with the print
statement. Consider the following
Complete details on the use of the formatting operator is provided in the next chapter.