在Python中,输出格式:%d , m , %-6d, d , %.6f的一些区分

本文通过Python代码展示了如何计算1到100的整数之和,并介绍了不同格式化输出的方法,包括整数输出、浮点数输出等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

和C/C++编程语言一样

%d 普通的整数输出

i = 1
sum = 0
while i <= 100:
    sum += i
    i += 1

print("1到100的和为:%d" % sum)


#  1到100的和为:5050

%6d 整数输出,整数的宽度是6位,若不足6位,左边补空格

 1 i = 1
 2 sum = 0
 3 while i <= 100:
 4     sum += i
 5     i += 1
 6 
 7 print("1到100的和为:%6d" % sum)
 8 
 9 # 1到100的和为:  5050 
10 # 5050前面空2位,一共6位

%-6d 整数输出,整数的宽度是6位,若不足6位,右边补空格

i = 1
sum = 0
while i <= 100:
    sum += i
    i += 1

print("1到100的和为:%-6d" % sum)


#  1到100的和为:5050  
#  其实5050后面有2个空格,这里没法显示,可以用鼠标拖黑一下就看见了

 

%6d 整数输出,整数的宽度是6位,若不足6位,左边补数字"0"

i = 1
sum = 0
while i <= 100:
    sum += i
    i += 1

print("1到100的和为:%06d" % sum)

#  1到100的和为:005050

%.6f 输出小数,即保留小数点后6位

i = 1
sum = 0
while i <= 100:
    sum += i
    i += 1

print("1到100的和为:%.6f" % sum)

#  1到100的和为:5050.000000

 

 

 

如果你和我有共同爱好,我们可以加个好友一起交流!

Jun 10 16:44:17 localhost dhcpd[2425759]: DHCPACK to 10.208.213.34 (8c:ec:4b:8f:2c:7f) via bond0 Jun 10 16:44:17 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:42ff port 547, link address fd5b:8173:ee5::2:1:42ff, peer address fe80::8023:5c2a:cf59:fab7 Jun 10 16:44:17 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5eff port 547, link address fd5b:8173:ee5::2:1:5eff, peer address fe80::3cee:dab:5737:d3b1 Jun 10 16:44:18 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:cff port 547, link address fd5b:8173:ee5::2:1:cff, peer address fe80::d2b2:ae46:55d2:45c5 Jun 10 16:44:18 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:41ff port 547, link address fd5b:8173:ee5::2:1:41ff, peer address fe80::a0d6:e376:1f16:c82b Jun 10 16:44:19 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5dff port 547, link address fd5b:8173:ee5::2:1:5dff, peer address fe80::a382:cc47:78aa:b1b7 Jun 10 16:44:19 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5ff port 547, link address fd5b:8173:ee5::2:1:5ff, peer address fe80::7e7d:1606:6dab:a4c4 Jun 10 16:44:20 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:2ff port 547, link address fd5b:8173:ee5::2:1:2ff, peer address fe80::89:a07b:8f1e:adc3 Jun 10 16:44:20 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:4cff port 547, link address fd5b:8173:ee5::2:1:4cff, peer address fe80::e6a0:e42e:2b9e:c14f Jun 10 16:44:20 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5eff port 547, link address fd5b:8173:ee5::2:1:5eff, peer address fe80::eaad:992f:9f6e:fa7f Jun 10 16:44:21 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:cff port 547, link address fd5b:8173:ee5::2:1:cff, peer address fe80::34ca:2591:e4da:6167 Jun 10 16:44:21 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:7ff port 547, link address fd5b:8173:ee5::2:1:7ff, peer address fe80::3b93:9ada:c80f:8ef1 Jun 10 16:44:21 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:52ff port 547, link address fd5b:8173:ee5::2:1:52ff, peer address fe80::a4e7:3c4f:8d41:c9af Jun 10 16:44:21 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5eff port 547, link address fd5b:8173:ee5::2:1:5eff, peer address fe80::ba44:9a54:7b13:50bb Jun 10 16:44:21 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5aff port 547, link address fd5b:8173:ee5::2:1:5aff, peer address fe80::10d4:12da:b7fc:7ab0 Jun 10 16:44:21 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5eff port 547, link address fd5b:8173:ee5::2:1:5eff, peer address fe80::9463:a005:aa71:f09d Jun 10 16:44:22 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:43ff port 547, link address fd5b:8173:ee5::2:1:43ff, peer address fe80::97a0:9225:f2a1:8eb2 Jun 10 16:44:22 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:47ff port 547, link address fd5b:8173:ee5::2:1:47ff, peer address fe80::4134:c623:9df5:86e Jun 10 16:44:22 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:cff port 547, link address fd5b:8173:ee5::2:1:cff, peer address fe80::be78:14b:9189:42d1 Jun 10 16:44:22 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5dff port 547, link address fd5b:8173:ee5::2:1:5dff, peer address fe80::1c9c:3495:98ba:daf Jun 10 16:44:22 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:44ff port 547, link address fd5b:8173:ee5::2:1:44ff, peer address fe80::45ed:8be8:8653:9890 Jun 10 16:44:23 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:6ff port 547, link address fd5b:8173:ee5::2:1:6ff, peer address fe80::e1b5:1280:5a49:9bd8 Jun 10 16:44:23 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:40ff port 547, link address fd5b:8173:ee5::2:1:40ff, peer address fe80::262:db4d:cc13:930 Jun 10 16:44:23 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:4dff port 547, link address fd5b:8173:ee5::2:1:4dff, peer address fe80::43db:d83:f742:5099 Jun 10 16:44:24 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5dff port 547, link address fd5b:8173:ee5::2:1:5dff, peer address fe80::ec1a:3c38:1ba1:ddc0 Jun 10 16:44:24 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:4fff port 547, link address fd5b:8173:ee5::2:1:4fff, peer address fe80::3773:7937:329c:fec9 Jun 10 16:44:24 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:3fff port 547, link address fd5b:8173:ee5::2:1:3fff, peer address fe80::164:7b5d:ffdf:a92a Jun 10 16:44:24 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:43ff port 547, link address fd5b:8173:ee5::2:1:43ff, peer address fe80::b6b9:b543:98ad:41d1 Jun 10 16:44:24 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:47ff port 547, link address fd5b:8173:ee5::2:1:47ff, peer address fe80::e1ea:b462:4942:6227 Jun 10 16:44:24 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:4dff port 547, link address fd5b:8173:ee5::2:1:4dff, peer address fe80::41af:ceeb:d9ab:f0ee Jun 10 16:44:25 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:bff port 547, link address fd5b:8173:ee5::2:1:bff, peer address fe80::bc73:357:c213:e052 Jun 10 16:44:25 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5dff port 547, link address fd5b:8173:ee5::2:1:5dff, peer address fe80::cfda:5978:ceda:2992 Jun 10 16:44:25 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:54ff port 547, link address fd5b:8173:ee5::2:1:54ff, peer address fe80::fd1c:b321:e5f4:6d Jun 10 16:44:26 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5dff port 547, link address fd5b:8173:ee5::2:1:5dff, peer address fe80::d893:d52a:d7a8:f608 Jun 10 16:44:26 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:47ff port 547, link address fd5b:8173:ee5::2:1:47ff, peer address fe80::6c32:8b2a:3e0f:bd03 Jun 10 16:44:26 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5eff port 547, link address fd5b:8173:ee5::2:1:5eff, peer address fe80::108c:4487:30d2:13a0 Jun 10 16:44:26 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:6ff port 547, link address fd5b:8173:ee5::2:1:6ff, peer address fe80::8f6d:b699:9d25:bff Jun 10 16:44:26 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:cff port 547, link address fd5b:8173:ee5::2:1:cff, peer address fe80::b3ff:9fbf:2cec:2a72 Jun 10 16:44:27 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:2ff port 547, link address fd5b:8173:ee5::2:1:2ff, peer address fe80::a752:e462:6645:5983 Jun 10 16:44:27 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5eff port 547, link address fd5b:8173:ee5::2:1:5eff, peer address fe80::e5aa:71c2:da37:7a1d Jun 10 16:44:27 localhost dhcpd[2425759]: DHCPREQUEST for 10.208.140.229 from 00:07:3e:a7:95:58 (admin) via bond0 Jun 10 16:44:27 localhost dhcpd[2425759]: DHCPACK on 10.208.140.229 to 00:07:3e:a7:95:58 (admin) via bond0 Jun 10 16:44:27 localhost dhcpd[2425759]: DHCPDISCOVER from 88:cf:98:06:b5:12 via 10.208.212.254 Jun 10 16:44:27 localhost dhcpd[2425759]: DHCPOFFER on 10.208.212.1 to 88:cf:98:06:b5:12 via 10.208.212.254 Jun 10 16:44:29 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5dff port 547, link address fd5b:8173:ee5::2:1:5dff, peer address fe80::b821:a833:f265:1906 Jun 10 16:44:31 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5dff port 547, link address fd5b:8173:ee5::2:1:5dff, peer address fe80::748b:4284:ed30:3b9d Jun 10 16:44:32 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5eff port 547, link address fd5b:8173:ee5::2:1:5eff, peer address fe80::8fe7:e6b7:de50:a83f Jun 10 16:44:33 localhost dhcpd[2425759]: DHCPINFORM from 10.208.213.1 via 10.208.213.254 Jun 10 16:44:33 localhost dhcpd[2425759]: DHCPACK to 10.208.213.1 (c0:3f:d5:7d:0f:13) via bond0 Jun 10 16:44:33 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:7ff port 547, link address fd5b:8173:ee5::2:1:7ff, peer address fe80::47c0:126a:a1f2:6b17 Jun 10 16:44:33 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5eff port 547, link address fd5b:8173:ee5::2:1:5eff, peer address fe80::d29c:a7dd:30e3:7a1f Jun 10 16:44:34 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:54ff port 547, link address fd5b:8173:ee5::2:1:54ff, peer address fe80::9601:fda8:e6b3:4368 Jun 10 16:44:34 localhost dhcpd[2425759]: DHCPREQUEST for 10.208.221.42 from 00:07:3e:a6:7f:75 (user-pc) via bond0 Jun 10 16:44:34 localhost dhcpd[2425759]: DHCPACK on 10.208.221.42 to 00:07:3e:a6:7f:75 (user-pc) via bond0 Jun 10 16:44:34 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5eff port 547, link address fd5b:8173:ee5::2:1:5eff, peer address fe80::c714:a68c:960a:2d6f Jun 10 16:44:34 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:45ff port 547, link address fd5b:8173:ee5::2:1:45ff, peer address fe80::94da:dac9:2644:55f2 Jun 10 16:44:35 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:55ff port 547, link address fd5b:8173:ee5::2:1:55ff, peer address fe80::3087:d15e:7266:f1ae Jun 10 16:44:35 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:2ff port 547, link address fd5b:8173:ee5::2:1:2ff, peer address fe80::2a91:b404:6506:2330 Jun 10 16:44:35 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:47ff port 547, link address fd5b:8173:ee5::2:1:47ff, peer address fe80::5c72:3c7f:8346:d894 Jun 10 16:44:35 localhost dhcpd[2425759]: DHCPREQUEST for 10.208.131.67 from 08:3a:88:c8:87:dc (wzsxubing-PC) via bond0 Jun 10 16:44:35 localhost dhcpd[2425759]: DHCPACK on 10.208.131.67 to 08:3a:88:c8:87:dc (wzsxubing-PC) via bond0 Jun 10 16:44:35 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:4eff port 547, link address fd5b:8173:ee5::2:1:4eff, peer address fe80::3610:cda7:be1:17c7 Jun 10 16:44:35 localhost dhcpd[2425759]: DHCPREQUEST for 10.208.194.81 from 00:07:3e:a6:85:b4 (admin) via bond0 Jun 10 16:44:35 localhost dhcpd[2425759]: DHCPACK on 10.208.194.81 to 00:07:3e:a6:85:b4 (admin) via bond0 Jun 10 16:44:36 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:55ff port 547, link address fd5b:8173:ee5::2:1:55ff, peer address fe80::3087:d15e:7266:f1ae Jun 10 16:44:36 localhost dhcpd[2425759]: DHCPREQUEST for 10.208.135.99 from 00:07:3e:a6:7c:64 (admin) via bond0 Jun 10 16:44:36 localhost dhcpd[2425759]: DHCPACK on 10.208.135.99 to 00:07:3e:a6:7c:64 (admin) via bond0 Jun 10 16:44:37 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:4cff port 547, link address fd5b:8173:ee5::2:1:4cff, peer address fe80::d28:2075:5252:f47e Jun 10 16:44:37 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:eff port 547, link address fd5b:8173:ee5::2:1:eff, peer address fe80::bdec:2ac8:d4c6:7c7c Jun 10 16:44:38 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:55ff port 547, link address fd5b:8173:ee5::2:1:55ff, peer address fe80::3087:d15e:7266:f1ae Jun 10 16:44:38 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:5aff port 547, link address fd5b:8173:ee5::2:1:5aff, peer address fe80::f61b:1cff:32d3:97ec Jun 10 16:44:38 localhost dhcpd[3989756]: Relay-forward message from fd5b:8173:ee5::2:1:44ff port 547, link address fd5b:8173:ee5::2:1:44ff, peer address fe80::d9cd:a8b2:9c9a:bc23 请分析日志信息
06-11
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值