1. input('str')
1 # coding=utf-8 2 # 获取用户信息并输出 3 4 name = input('name:') 5 age = int(input('age:')) #input默认得到字符串 6 job = input('job:') 7 8 info = ''' 9 姓名:%s 10 年龄:%d 11 职业:%s 12 ''' % (name, age, job) 13 14 info2 =''' 15 name:{_name} 16 age:{_age} 17 job:{_job} 18 '''.format(_name = name, _age = age, _job = job) 19 20 info3 = ''' 21 name:{2} 22 age:{1} 23 job:{0} 24 '''.format(name, age, job) 25 print(info) 26 print(info2) 27 print(info3) #注:input()获得的数据类型为‘str’
2. 暗码获取输入
1 # 判断用户舒服的用户名和密码是否正确 2 3 import getpass 4 5 _username = 'zhangfei' 6 _password = '123456' 7 8 username = input('username:') 9 password = getpass.getpass('password:') #暗码获取密码 10 if username == _username and password == _password: 11 print('Welcome user {name} login...'.format(name = username)) 12 else: 13 print('Invalid username or password')
3. 获得变量值的方法
>>> s = "some numbers:" >>> x = 1.34 >>> y = 2 # 用百分号隔开,括号括起来 >>> "%s %f, %d" % (s, x, y) 'some numbers: 1.340000, 2' >>> '{} {} {}'.format('a', 'b', 'c') 'a b c' >>> '{2} {1} {0}'.format('a', 'b', 'c') 'c b a' >>> '{color} {n} {x}'.format(n=10, x=1.5, color='blue') 'blue 10 1.5' >>> from math import pi >>> '{0:10} {1:10d} {2:10.2f}'.format('foo', 5, 2 * pi) 'foo 5 6.28' #参数可以嵌套,顺序由外到内 >>> 'apple={:.{}f}'.format(12.12345, 3) apple=12.123