Python user interaction program and formatted output

Source: Internet
Author: User
Tags string format

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.