The use of modules in Python 3

When running a Python program, Python's standard library modules are always used. Some standard library modules are embedded into the Python interpreter, and special functions can be realized by calling the functions provided by these modules. The sys module is a standard library module of Python, which is embedded into the Python interpreter.

1 import of sys module

Through the import statement, the sys module can be imported in the Python program, and the code is as follows.

import sys

2 Modify the prompt of the Python command line

Modify the prompt of the Python command line through the ps1 and ps2 variables in the sys module.

It should be noted that IDLE's command line is not strictly a Python command line, so sys.ps1 and sys.ps2 cannot be used in IDLE's command line mode.

2.1 Enter the Python environment

Click the "Win" + "R" keys on the keyboard, and enter "cmd" in the "Open" box of the "Run" window that pops up in the lower right corner of the screen.

"Click the "OK" button, as shown in Figure 1.

Figure 1 "Run" window

After that, enter the "cmd" window, enter "python" in the command line, and click the "Enter" button to enter the Python environment, as shown in Figure 2.

Figure 2 Enter the python environment

2.2 View prompt

Use the following code to view the current first-level prompt and second-level prompt in the Python environment, as shown in Figure 3.

Figure 3 View the first-level prompt and the second-level prompt

As can be seen from the above code, the current first-level prompt of the Python command line is ">>>", and the second-level prompt is "...".

Related links 1  The first-level prompt and second-level prompt of the Python command line are shown in Figure 4① and Figure 4②.

Figure 4 Level 1 Prompt and Level 2 Prompt

2.3 Modify prompt

Use the code shown in Figure 5 to modify the first-level prompt and the second-level prompt.

Figure 5 Modify the first-level prompt and the second-level prompt

As can be seen from the above code, the first-level prompt of the Python command line is changed to "Please enter the command:", and the second-level prompt is changed to "Please continue to enter the command".

It should be noted that after exiting the Python environment and re-entering, the first-level prompt and the second-level prompt will be restored.

Guess you like

Origin blog.csdn.net/hou09tian/article/details/130894056