码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-10-26 14:22:17      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:span   orm   import   字符串拼接   integer   word   size   use   int   

  用户输入

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】用户输入

标签:span   orm   import   字符串拼接   integer   word   size   use   int   

原文地址:http://www.cnblogs.com/a1-code/p/5999893.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!