首页 > 代码库 > Python 学习笔记【04】用户输入

Python 学习笔记【04】用户输入

  用户输入

1 name = input("username:")
2 password = input("password:")
3 
4 print(name, password)

  其他示例1:字符串拼接

 1 name = input("Name:")
 2 age = input("Age:")
 3 job = input("Job:")
 4 salary = input("Salary:")
 5 
 6 
 7 info = ‘‘‘
 8 ----------info of ‘‘‘ + name + ‘‘‘ ----------
 9 Name: ‘‘‘ + name + ‘‘‘
10 Age: ‘‘‘ + age + ‘‘‘
11 Job: ‘‘‘ + job + ‘‘‘
12 Salary: ‘‘‘ + salary + ‘‘‘
13 ‘‘‘
14 
15 print(info)

  其他示例2:用占位符

 1 name = input("Name:")
 2 age = input("Age:")
 3 job = input("Job:")
 4 salary = input("Salary:")
 5 
 6 info = ‘‘‘
 7 ----------info of  %s----------
 8 Name: %s
 9 Age: %s
10 Job: %s
11 Salary: %s
12 ‘‘‘ % (name, name, age, job, salary)
13 
14 print(info)

字符串是 %s; 整数 %d; 浮点数%f

  打印数据类型

1 age = input("age:")
2 print(type(age))

  强制转换数据类型

1 age = int(input("age:")) #integer
2 print(type(age),type(str(age)))

  其他示例3

 1 name = input("Name:")
 2 age = input("Age:")
 3 job = input("Job:")
 4 salary = input("Salary:")
 5 
 6 info = ‘‘‘
 7 ----------info of {_name}----------
 8 Name: {_name}
 9 Age: {_age}
10 Job: {_job}
11 Salary: {_salary}
12 ‘‘‘ .format(_name=name,
13             _age=age,
14             _job=name,
15             _salary=salary)
16 
17 print(info)

  其他示例4

 1 name = input("Name:")
 2 age = input("Age:")
 3 job = input("Job:")
 4 salary = input("Salary:")
 5 
 6 info = ‘‘‘
 7 ----------info of {0}----------
 8 Name: {0}
 9 Age: {1}
10 Job: {2}
11 Salary: {3}
12 ‘‘‘ .format(name, age, job, salary)
13 
14 print(info)

  密文输入密码

1 import getpass
2 
3 name = input("username:")
4 password = getpass.getpass("password:")
5 
6 print(name, password)

Python 学习笔记【04】用户输入