本章涉及到用户交互输入输出,以及格式化输出,变量等。
要求输出如下格式的信息:
---------- info of Alex ---------- name:Alex age:12 sex:male company:Google
方式1:
字符串拼接,每个变量一一对应
name = input("name:") age = input("age:") sex = input("sex:") company = input("company:") info1 = ''' ---------- info of ''' + name + ''' ---------- name:''' + name + ''' age:''' + age + ''' sex:''' + sex + ''' company: ''' + company + ''' ''' print(info1)
方式2:
采用格式符方式占位:%s, %d, %f等,每个输入的数据类型必须一致。
%s表示为字符串,%d表示为十进制的整数,%f表示浮点型。
name = input("name:") age = int(input("age:")) sex = input("sex:") company = input("company:") info2 = ''' ---------- info of %s ---------- name:%s age:%d sex:%s company: %s ''' % (name, name, age, sex, company) print(info2)
方式3:
name = input("name:") age = int(input("age:")) sex = input("sex:") company = input("company:") info3 = ''' ---------- info of {_name} ---------- name:{_name} age:{_age} sex:{_sex} company: {_company} '''.format(_name=name, _age=age, _sex=sex, _company=company) print(info3)
方式4:
name = input("name:") age = int(input("age:")) sex = input("sex:") company = input("company:") info4 = ''' ---------- info of {0} ---------- name:{0} age:{1} sex:{2} company: {3} '''.format(name, age, sex, company) print(info4)