Home

python 输出

2019/03/30

python 输出

##1、用“+”输出:

1
2
3
4
a = 'hello'
b = 'word'
print(a+' '+b+'!')
#输出:hello word!

输出中使用“*”:

1
print((a+' '*10))

输出结果为:
image

##2、标准化输出:

1
2
3
i = 0.999
#输出百分号需要用%%
print("一定要看%.1f%%这部剧!" % (i*100))

结果为:
image

常用输出:

格式化符号 说明
%o oct 八进制
%d dec 十进制
%x hex 十六进制
%f 转成浮点数
%e 转成科学计数法
%g 指数e+或浮点数 (根据显示长度)
%% 输出字符%

##2、format格式化函数:

1
2
3
4
5
6
7
a = "hello"
b = "world"
print("{1} {0} {1}".format(a,b))
#输出:word hello word
listA = ["hello","hi","!"]
print("{0[0]}{0[2]} {0[1]}{0[2]}".format(listA)) #{0[1]}前面那个0是必须的!
#输出:hello! hi!

格式转化:
image