Why input and output?
Both the “Hello World” from the beginning and the various previous examples are basically “speaking for themselves”, showing snippets of code for the class. Only can receive user input, according to the input code to generate results, and output to the screen display, just a relatively complete, simple program.
Input function
Gets user input and saves it as a string. The return value of the input function is a string. Even if you enter a number 1, you will only be returned the string “1”, not the integer 1. Here are some simple demo examples:
In the first example, inp = input(” Please input your name: “). Input provides a string to prompt the user for input. After the return value of the input function is assigned to the variable INP, inP holds the value entered by the user.
Type is one of Python’s built-in functions that is very useful for viewing the data type of an object.
In the second example, age 18 is entered, but age holds a string of “18”.
The third example, with no input, returns an empty string.
In the fourth example, the whitespace before and after the valid input is preserved in the returned string.
As you can see from the example above, the input function returns the user’s input to the variable unchanged, wrapped as a string. This is definitely not going to work, and can cause a lot of problems, so user input often needs to be processed and judged.
- Processing null input:
- To convert a string to a numeric type:
- Remove the blank lstrip at the beginning, the blank Rstrip at the end, and the blank strips at both ends
- Determines the character type of the input
Int (); int(); int(); int();
For strings such as “123” and “283242”, conversion is ok, but for strings containing characters, special characters cannot be converted, will pop up an exception error. So before we use int, we need to evaluate the input.
Modify the above example:
- The input function can sometimes be cleverly used to block or pause a program
The input function does not save the input, but only pauses the program action.
Python is full of me
Print input function
The print function is used to format the content for display on standard output, mainly the on-screen monitor.
Print can accept multiple strings, string variables, or printable objects. Each string is separated by commas (,) to form a string of output. Print prints each string in turn and a space for each comma.
Print (a+”a”+b) print(a+”a”+b) print(a+”a”+b) Print () automatically executes the internal statement and prints the desired result. Here’s another example:
Take a look at the prototype print function:print(self, *args, sep=' ', end='\n', file=None)
Sep argument: delimited symbol, default is a space;
The end argument: the end of the print. The default is newline character \n. If we set end=”, we can print consecutively on one line without newlines. Flexible print control can be realized by using the parameters of print.
Print format output
After Python2.6, there are two ways to format output.
One is the % % format of printf, similar to C, which is Python’s most basic and common formatter. The other is the str.format() method.
Here we focus on the traditional % percent formatting output,
Take the following statement as an example:
First construct a string “my name is %s this year % D!” , replace the parts that need to be replaced by other variables or values with % % characters and a data type code, such as %s, %d. You then append the string with % to a tuple of the same number of variables or values.
That is, how many parameter values are provided after the number of % signs, each parameter value is separated by commas, all parameters are enclosed in parentheses. Each argument corresponds to the preceding % one by one, and the data type must be valid.
This is the basic usage, see the picture below for more formats, and then learn how to match the formats.
Formatting symbol:
Formatting operator auxiliary instructions:
Classic case:
In particular, if you want to print the %% symbol itself in print, then you need to use %%. Two % characters escape from one % character, namely print(“%%”).
If you have no love for print’s complicated formatting controls and no aesthetics, it’s a no-brainer to simply use %s as a placeholder along the way. Such as:
Five, the summary
This article is based on Python, introduces several common input and output methods, through the actual operation of a project, can better understand the use of input and output statements. Difficulties encountered in the process of project practice and points needing attention are explained in detail.
Python is full of me