python flag用法_【flag】 python 格式化字符 and help用法

python格式化应用

九九乘法表:

1 defprintLine(row):2 for col in range(1,row+1):3 print(row*col,end=' ')4 print('')5

6 for row in range(1,10):7 printLine(row)

执行结果:

1

2 4

3 6 9

4 8 12 16

5 10 15 20 25

6 12 18 24 30 36

7 14 21 28 35 42 49

8 16 24 32 40 48 56 64

9 18 27 36 45 54 63 72 81

注意到由于有的数字仅有一位 有的两位 我的乘法表没对齐 好难看!!!

修改:

1 defprintLine(row):2 for col in range(1,row+1):3 print('{0:2}'.format(row*col),end=' ')4 print('')5

6 for row in range(1,10):7 printLine(row)

执行结果:

1

2 4

3 6 9

4 8 12 16

5 10 15 20 25

6 12 18 24 30 36

7 14 21 28 35 42 49

8 16 24 32 40 48 56 64

9 18 27 36 45 54 63 72 81

利用 format()函数进行格式化

取位数“{:2}”、"{:.2f}"等 #分别表示 保留两位数字 /小数点后保留两位数字

- 格式化字符参考文章:

https://www.cnblogs.com/fat39/p/7159881.html

一些细节 :

1. end=' '

- print 打印完成后默认换行(默认参数)

- 关于参数的查找与修改:

- 利用help()函数

help(print)

结果:

Help on built-in function print inmodule builtins:print(...)print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream,orto sys.stdout by default.

Optional keyword arguments:

file: a file-like object (stream); defaults to the current sys.stdout.

sep: string inserted between values, default a space.

end: string appended after the last value, default a newline.

flush: whether to forcibly flush the stream.

据此修改参数

2.print('') (代码块第四行)

再次利用print的默认参数仅进行换行

3.函数是个好东西 在重复型任务方面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值