The main contents of this section:
- Example one input ()
- Example of two-character stitching
- Example of a three-placeholder
- Example four raw_input () and input ()
- Example five formatting of user interaction
- Example six array formatting
- Reference pages
The user uses the input function to interact, and this section learns this section with examples:
Example one input ()
#!/usr/bin/env python # -*- coding:utf-8 -*-# Author:Cathy Wuusername =input("username")password =input("password")print(username,password)
After running, you are prompted to enter "username" and "password", and then print "username" and "password" after entering.
Example of two-character stitching
name = input("name")age = input("age")salary = input("salary")info = ‘‘‘------info of ‘‘‘ + name+‘‘‘---age:‘‘‘ + age + ‘‘‘salary:‘‘‘+ salaryprint(info)
Operation Result:
namecathyage25salary5000------info of cathy---age:25salary:5000
Example of a three-placeholder
%s or%d corresponds to a placeholder symbol, which corresponds to each one. s represents the String,d, and%f represents the floating-point number. All input by default is string.
name = input("name")age = input("age")salary = input("salary")info =‘‘‘---info of %s ----name: %sage:%ssalary: %s‘‘‘ % (name,name,age,salary)print(info)
Operation Result:
namecathyage12salary12---info of cathy ----name: cathyage:12salary: 12
Example four raw_input () and input ()
There is no raw_input inside Python3. Raw_input is used in Python2, input () is essentially implemented using raw_input (), just call the eval () function after calling Raw_input ().
>>> raw_input_a = Raw_input ("Raw_input:")Raw_input:abc>>> input_a = input ("Input:")Input: ABCTraceback (most recent):File "<pyshell#1>", line 1, in <module> input_a = input ( " Input: ") file " <string> ", line 1, in < module>nameerror: Name ' abc ' is not defined >>> input_a = input ( "input:") input: "abc" >>>
>>> raw_input_b = raw_input ( "raw_input:") Raw_input: 123 >>> type (raw_input_b) <type ' str ' >>>> input_b = input ( " Input: ") input: 123>>> < Span class= "Hljs-title" >type (input_b) <type ' int ' >> >>
The above code can see: Raw_input () treats all input as a string and returns the string type. While input () has its own characteristics when dealing with a pure numeric input, it returns the type of the number entered (int, float), and at the same time the previous code knows that input () accepts a valid Python expression, for example: input (1 + 3) returns an int of type 4 。
Example five formatting of user interaction
The official recommended method of user use.
name = input("name")age = input("age")salary = input("salary")info3 = ‘‘‘---info3 of {_name} ----name: {_name}age: {_age}salary: {_salary}‘‘‘.format(_name=name, _age=age, _salary=salary)print(info3)
The results of the operation are as follows:
namecathyage25salary10000---info3 of cathy ----name: cathyage: 25salary: 10000
Example six array formatting
name = input("name")age = input("age")salary = input("salary")info4 = ‘‘‘---info4 of {0} ----name: {0}age: {1}salary: {2}‘‘‘.format(name, age, salary)print(info4)
The results of the operation are as follows:
namecathyage25salary15000---info4 of cathy ----name: cathyage: 25salary: 15000
Other
Example of appeal, the password is visible, how to make the password is not seen, there is a module getpass
input("username:")password = getpass.getpass("password")print(username, password)
Reference pages
Http://www.cnblogs.com/way_testlife/archive/2011/03/29/1999283.html
Python Basic 4 user interaction