首页 > 代码库 > python基础 Day01 练习题

python基础 Day01 练习题

1 字符串格式化

技术分享
 1 #!/urs/bin/env python
 2 
 3 name = input("Name: ")
 4 age = int(input("Age: "))
 5 job = input("Job: ")
 6 salary = input("Salary: ")
 7 
 8 info1 = """
 9 
10 ----------- info1 of %s ---------
11 name: %s
12 age: %s
13 job: %s
14 Salary: %s
15 """ % (name, name, age, job, salary)
16 
17 print(info1)
18 
19 info2 = """
20 
21 ----------- info2 of {_name} ----
22 Name: {_name}
23 Age: {_age}
24 Job: {_job}
25 Salary: {_salary}
26 """.format(_name=name,
27            _age=age,
28            _job=job,
29            _salary=salary)
30 
31 print(info2)
32 
33 info3 = """
34 
35 ------ info3 of {0} -------
36 Name: {0}
37 Age: {1}
38 Job: {2}
39 Salary: {3}
40 """.format(name, age, job, salary)
41 
42 print(info3)
View Code

2 密码非明文显示

技术分享
1 #!/urs/bin/env python
2 # -*- coding:utf-8 -*-
3 
4 import getpass
5 username = input("username:")
6 password = getpass.getpass("password:")
7 
8 print(username)
9 print(password)
View Code

注在Pycharm中不可以,在命令行模式运行:python xxx.py可以

 

python基础 Day01 练习题