Skip to content
On this page

Input and Output

Output

Using print() with a string inside the parentheses allows you to output specified text to the screen. For example, to output 'hello, world', use the following code:

python
>>> print('hello, world')

The print() function can also accept multiple strings separated by commas ,, allowing you to concatenate and output them as a single string:

python
>>> print('The quick brown fox', 'jumps over', 'the lazy dog')
The quick brown fox jumps over the lazy dog

print() will sequentially print each string, and when it encounters a comma ,, it outputs a space. Therefore, the output string is assembled as follows:

print.jpg

print() can also display integers or the results of calculations:

python
>>> print(300)
300
>>> print(100 + 200)
300

Thus, we can make the output of 100 + 200 look a bit nicer:

python
>>> print('100 + 200 =', 100 + 200)
100 + 200 = 300

Note: For 100 + 200, the Python interpreter automatically calculates the result 300. However, '100 + 200 =' is a string, not a mathematical expression. Python treats it as a string, so please interpret the above print result accordingly.

Input

Now that you can use print() to output the results you want, what if you want the user to input some characters into the computer? Python provides input(), which allows the user to enter a string and store it in a variable. For example, to input the user's name:

python
>>> name = input()
Michael

When you enter name = input() and press Enter, the Python interactive command line waits for your input. At this point, you can enter any characters and then press Enter to complete the input.

After the input is completed, there will be no prompt, and the Python interactive command line returns to the >>> state. So, where did the content you just entered go? The answer is that it was stored in the name variable. You can directly enter name to view the contents of the variable:

python
>>> name
'Michael'

What is a variable? Please recall the basic algebra knowledge learned in middle school:

If the side length of a square is a, then the area of the square is a x a. Treating the side length a as a variable, we can calculate the area of the square based on the value of a. For example:

  • If a = 2, then the area is a x a = 2 x 2 = 4;
  • If a = 3.5, then the area is a x a = 3.5 x 3.5 = 12.25.

In computer programs, variables can hold not only integers or floating-point numbers but also strings. Therefore, name as a variable is a string.

To print the contents of the name variable, you can either directly type name and press Enter or use the print() function:

python
>>> print(name)
Michael

With input and output capabilities, we can modify the previous 'hello, world' program to make it more meaningful:

python
name = input()
print('hello,', name)

Running the above program, the first line of code allows the user to input any characters as their name, which is then stored in the name variable. The second line of code greets the user based on their name. For example, if the user inputs Michael:

C:\Workspace> python hello.py
Michael
hello, Michael

However, when the program runs, there is no prompt informing the user to "Hey, please enter your name," which makes it seem unfriendly. Fortunately, input() can display a string to prompt the user. Therefore, we can modify the code as follows:

python
name = input('please enter your name: ')
print('hello,', name)

Running this program again, you will notice that when the program starts, it first prints please enter your name: , allowing the user to input their name based on the prompt and receive the hello, xxx output:

C:\Workspace> python hello.py
please enter your name: Michael
hello, Michael

Each time the program runs, the output will vary based on the user's input.

Input and output in the command line are this simple.

Summary

Every computer program is designed to perform a specific task. With input, users can provide the necessary information to the program, and with output, the program can inform users of the results of the task.

Input is Input, and output is Output, so collectively, we refer to them as Input/Output, or simply IO.

input() and print() are the most basic input and output functions in the command line. However, users can also perform input and output through more advanced graphical interfaces, such as entering their name in a text box on a webpage and clicking "Submit" to see the output information on the webpage.

Exercise

Please use print() to output 1024 * 768 = xxx:

python
# TODO:
print(???)
Input and Output has loaded