python print format,颜色,%d%s%f用法速查

python 2 和python 3 统一小窍门,python 2 加入下面一行代码

from __future__ import print_function

字面意思即为,从未来加载模块。

warming up

print('size of image:',256)

('size of image:', 256)

print 'Price of eggs: $%d' % 42

Price of eggs: $42

(C语言格式,‘d%’被替换)


怎么换行?

print(2,3,4,'\n',5,6,7)
print("When it gets hard, that's \n when he has got to stick it out.")

2 3 4
 5 6 7
When it gets hard, that's
 when he has got to stick it out.


format用法

大括号!'{ }'.format( )。{}其实是set。

a= 1
b = 2

a = a + b
b = a - b
a = a - b
print ('a = {0}, b = {1}'.format(a,b))

print ("a={1} b={0} {1}".format("hello", "world")) 

a = a ^ b
b = a ^ b
a = a ^ b
print 'a=%d' %a +',b=%d' %b

输出

a = 2, b = 1
a=world b=hello world
a=1,b=2
 

上述植入了两种交换a和b值的例子,^是按位异或。

if i % 30 == 0:
    print("image:", '%04d' % (i+1),\
    "cost=", "{:.9f}".format(loss))

('image:', '0001', 'cost=', '3.899574518')
('image:', '0031', 'cost=', '2.469503164')
('image:', '0061', 'cost=', '1.211874366')
('image:', '0091', 'cost=', '0.633342564')
('image:', '0121', 'cost=', '0.465918243')

.9f 的意思是:dot后面是精度,如

>>>from math import pi

>>>"Pi with three decimals: %.3f"% pi

Pi with three decimals: 3.142

dot前是指定位宽,指的是整个数的位宽(包括小数位),这样做的目的之一是前面补0。

>>>'%04d'% pi
'0003'

>>>'%20.2f' % pi

 '                3.14'

不补0那么就是空格。这种用法在制表对齐时很有用。


k=2
validation_accuracy=0.3
print('epoch %d : complete,Validation accuracy = %.1f%%' % (k,validation_accuracy * 100.0))

epoch 2 : complete,Validation accuracy = 30.0%

acc = 0.759876
print("Epoch:", '%04d' % 1,
                  "cost=", "{:.9f}".format(20),
                  "Training accuracy","{:.5f}%".format(acc*100))

Epoch: 0001 cost= 20.000000000 Training accuracy 75.98760%


\033[1;033    #颜色打印(蓝色)

\033[0m    #重置print

patient_n=5
epoch=1
validation_accuracy = 0.8
print("\033[1;031m",end="")
print("Step: ", 2, "Cost: ",3, "IoU:", 4)
print('\033[1;034m Patient %d complete, epoch %d complete,Validation accuracy = %.1f%%' % (patient_n, epoch,validation_accuracy * 100.0))
print('\033[0m Saving...')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞行codes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值