1. User input
In Python 3, user interaction is implemented using the input () function.
For example, we enter the user name and password according to the program prompts, and print the input information.
2. String format output
For example, we enter information such as name, job, salary, etc. according to the program prompts, and then implement a variety of formatted output.
(1) with the addition of "+" to achieve formatted output.
The code is as follows:
1Name = input ("Name:")2Job = input ("Job:")3Salary = input ("Salary:")4 5Info1 =" "6 ----------Info1 of" "+ name +" "-----------7 Name:" "+ name +" "8 Job:" "+ Job +" "9 Salary:" "+SalaryTen Print(INFO1)
The results of the operation are as follows:
(2) Implement formatted output with%s.
1Name = input ("Name:")2Job = input ("Job:")3Salary = input ("Salary:")4 5Info2 =" "6 --------Info of%s-----7 name:%s8 job:%s9 salary:%sTen " "%(name,name,job,salary) One Print(info)
The results of the operation are as follows:
(3) Use variables to implement formatted output.
The code is as follows:
1Name = input ("Name:")2Job = input ("Job:")3Salary = input ("Salary:")4 5Info3 =" "6 --------Info3 of {_name}-----7 Name:{_name}8 Job:{_job}9 Salary:{_salary}Ten " ". Format (_name=name, One_job=Job, A_salary=salary) - Print(INFO3)
The results of the operation are as follows:
(4) Using parameters to achieve formatted output.
The code is as follows:
1Name = input ("Name:")2Job = input ("Job:")3Salary = input ("Salary:")4 5Info4 =" "6 --------Info4 of {0}-----7 name:{0}8 Job:{1}9 salary:{2}Ten " ". Format (name,job,salary) One Print(Info4)
The results of the operation are as follows:
From the above several implementations, the output results are the same effect. However, these implementations are all in memory to open up a memory space, so, no longer a last resort, avoid the use of string concatenation. In particular, the first way to stitch strings with a plus sign will open up a lot of memory space in memory, inefficient, and try to avoid the use of this method.
Python user interaction program and formatted output