Python input,

Source: Internet
Author: User

Python input,

Python Input

The input after python3 is different from that after python2.7. The input function is input ()

 

Username = input ("username"); password = input ("password"); print ("username", username); print ("password", password );

 

Let's talk about how to encrypt the password when it is entered. python has its own function:

import getpasspassword = getpass.getpass(input("password:"));

Getpass needs to be introduced, which is the basic module of python.

Note: The getpass module cannot be run in the Pycharm development tool. Use cmd to run it.

Finally, let's talk about formatting the output. here we need to use.

Sample Code 1:

name = input("name");age = input("age");job = input("job");salary = input("salary");
info = '''
----------- info of '''+ name + ''' -----------
name : ''' + name + '''
age : ''' + age + '''
job : ''' + job + '''
salary : ''' + salary + '''
'''

print(info)

The above code is '''....... ''' is used to connect strings. At this time, make sure that all your input items must be of the string type, and input is of the string type by default.

The following question is: What should I do if age is of the int type, and it is too troublesome to format the output of such a string connection?

Sample Code 2:

name = input("name");age = int(input("age"));job = input("job");salary = input("salary");
infos = '''
----------- info of %s -----------
name : %s
age : %d
job : %s
salary : %s
''' %(name,name,age,job,salary)

print(info)

Note: python provides a method for forced type conversion by adding an int before the input. The input content can be converted to int, where % s is of the string type, % d is int type.

However, there is still a problem in this writing: The content in % (name, name, age, job, salary) must correspond to the content above, and the number must be equal, at this time, we can further optimize it.

Sample Code 3:

name = input("name");age = int(input("age"));job = input("job");salary = input("salary");
info = '''
----------- info of {_name} -----------
name : {_name}
age : {_age}
job : {_job}
salary : {_salary}
''' .format(_name = name , _age = age , _job = job , _salary = salary)

print(info)

By calling the. format method, you can implement the above formatting output more flexibly. It is also a recommended method, which is a bit like the java constructor.

In addition, python also supports the following writing methods:

Sample Code 4:

name = input("name");age = int(input("age"));job = input("job");salary = input("salary");
info = '''
----------- info of {0} -----------
name : {0}
age : {1}
job : {2}
salary : {3}
''' .format(name, age ,job , salary)

print(info)

Finally, we will show you the output of the above Code:

----------- Info of admin -----------
Name: admin
Age: 20
Job: IT
Salary: 1000000.

This is the 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.