Lesson 1#08 格式化输出

输出格式:

----------Welcome xx into zone------
|Name:                           
|Age:                            
|Sex:                            
|Job:                            
-----------------END----------------

信息输出方式1:

1 name = input("Please input your name:") 
2 age = input("Please input your age:") 
3 sex = input("Please input your sex:") 
4 job = input("Please input your job:") 
5 
6 print("-----Welcome",name, "into zone----") 
7 print("Name:",name) 
8 print("Age:",age) 
9 print("Sex:",sex) 
10 print("Job:",job) 
11 print("---------------END--------------")

信息输出方式2:

 

1 name = input("Please input your name:") 
2 age = input("Please input your age:") 
3 sex = input("Please input your sex:") 
4 job = input("Please input your job:") 
5 info = '''
6 ----------Welcome %s into zone------ 
7 |Name:%s 
8 |Age:%s 
9 |Sex:%s 
10 |Job:%s 
11 -----------------END---------------- 
12 '''%(name,name,age,sex,job) 
13 #%s 代表字符串 
14 #%d 代表数值 
15 #使用input输入的任何字符都默认为字符串 
16 print(info)

 

 

 

要点解析:

1、使用input函数输入的任何字符默认都是字符串格式

1 1 age = input("Please input your age:")
2 2 print(age,type(age))
3 3 
4 4 Please input your age:18
5 5 18 <class 'str'>

2、如果需要输入其他类型,需要在input前面做类型定义,例如:

1 age = int(input("Please input your age:"))
2 print(age,type(age))
3 Please input your age:18 4 18 <class 'int'>

 

3、格式符


%% 百分号标记 #就是输出一个%
%c 字符及其ASCII码
%s 字符串 ,最常用的类型
%d 有符号整数(十进制),有正负符号
%u 无符号整数(十进制),无正负符号,只能输出正值
%o 无符号整数(八进制)
%x 无符号整数(十六进制)
%X 无符号整数(十六进制大写字符)
%e 浮点数字(科学计数法)
%E 浮点数字(科学计数法,用E代替e)
%f 浮点数字(用小数点符号)
%g 浮点数字(根据值的大小采用%e或%f)
%G 浮点数字(类似于%g)
%p 指针(用十六进制打印值的内存地址)
%n 存储输出字符的数量放进参数列表的下一个变量中


posted @ 2018-03-07 14:59  WudTime  阅读(124)  评论(0编辑  收藏  举报