Python Input and output


Use print () in parentheses with a string, you can output to the screen specified text. Such as the output ‘hello, world’, with the code as following:

>>> print('hello, world')

print () function can also accept multiple strings, separated by a comma “,” can be connected into a string of output:

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

print () will print each string in turn, encounter comma “,” will output a space, so the output string is so fight up:

Python Input and output

print()You can also print an integer, or calculate the result:

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

So we can print the results of 100 + 200 to print a little more beautiful:

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

Note that for the 100 + 200, the Python interpreter automatically evaluates the result 300, but ‘100 + 200 =’ is a string rather than a mathematical formula, and Python treats it as a string. Please explain the print results yourself.


Now, you can already use print () to output the results you want. But what if you want the user to enter some characters from the computer?

Python provides an input () that allows the user to enter a string and store it in a variable. Such as entering the user’s name:

>>> name = input()

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

After the input is complete, there will be no prompt, Python interactive command line back to >>> state of the. Where have we entered the content? The answer is stored in the name variable. You can directly enter the name of the variable content:

>>> name

What is a variable? Please remember the basic knowledge of algebra learned by junior high school mathematics:

If the length of the square is a, the area of the square is a x a. The length of a as a variable, we can calculate the value of a square area, such as:

If a = 2, then the area is: a x a = 2 x 2 = 4;

If a = 3.5, the area is: a x a = 3.5 x 3.5 = 12.25。

In a computer program, a variable can be either an integer or a floating-point number, or a string, so the name is a string as a variable.

To print out the contents of the name variable, in addition to writing directly and then press Enter, you can also use the print () function:

>>> print(name)

With the input and output, we can put the last print ‘hello, world’ program into a little sense of the program:

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

Run the above program, the first line of code allows the user to enter any character as their own name, and then stored in the name variable; the second line of code will be based on the user’s name to the user that hello, such as inputMichael

C:\Workspace> python
hello, Michael

But when the program is running, there is no prompt to tell the user: “Hey, hurry to enter your name”, it looks very unfriendly. Fortunately, input () allows you to display a string to prompt the user, so we changed the code to:

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

Run this program again, you will find that the program is running, will first print out please enter your name :, so that users can follow the prompts, enter the name, get hello, xxx output:

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

Each time the program is run, the output will be different depending on the user input.

In the command line, the input and output are so simple.


Any computer program is to perform a specific task, with the input, the user can tell the computer program the required information, with the output, the program can run to tell the results of user tasks.

Input is Input, the output is Output, so we input and output collectively referred to as Input / Output, or abbreviated as IO.

input () and print () are the most basic input and output below the command line, but the user can also input and output through other more advanced graphical interfaces, such as entering a name on a text box on the page, Click “OK” to see the output information on the page.