>>> '%s, %s!' % ('Hello', 'World')
>>> '{0}, {1}!'.format('Hello', 'World')
>>> '{}, {}!'.format('Hello', 'World')
'Hello, World!'
>>> format(12345678, ',')
>>> format(1234, ',').replace(',', '_')
>>> format(1234, ',').replace(',', '_')
>>> '{0:8,.1f}'.format(123.456)
>>> tt = bytes.maketrans(b'123', b'456')
>>> len(tt)
256
>>> tt
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\
t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\
x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\
x1e\x1f !"#$%&\'()*+,-./0456456789:;<=>
?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcd
efghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\
x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\
x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\
x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\
xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\
xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\
xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\
xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\
xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\
xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\
xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\
xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\
xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\
xfa\xfb\xfc\xfd\xfe\xff'
>>> b'123456'.translate(tt)
>>> b'123456'.translate(tt, b'5')
>>> tt = str.maketrans('123', '456')
{49: 52, 50: 53, 51: 54}
>>> '123456'.translate(tt)
'456456'
>>> int.bit_length(19) 5 >>> bin(19)
>>> round(1000) 1000 >>> round(1000.0) 1000 >>> round(1000, 2) 1000 >>> round(1000.0, 2)
>>> round(1000) 1000 >>> round(1000.0) 1000 >>> round(1000, 2) 1000 >>> round(1000.0, 2)
>>> 0.6
>>> 0.6
>>> x = 0.0 >>> while x != 1.0: ... print(repr(x)) ... x += 0.1输出的结果: 0 0.10000000000000001 0.20000000000000001 0.30000000000000004 0.40000000000000002 0.5 0.59999999999999998 0.69999999999999996 0.79999999999999993 0.89999999999999991 0.99999999999999989 1.0999999999999999 1.2 1.3 1.4000000000000001 1.5000000000000002 1.6000000000000003
>>> print(0.1) 0.1 >>> print(0.10000000000000001)
>>> from decimal import Decimal
>>> Decimal.from_float(0.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')
open('in.txt', 'w').write('abc def')
with open('in.txt') as in_file:
with open('out.txt', 'w') as out_file:
text = in_file.read()
assert text == 'abc def'
text = text.title()
assert text == 'Abc Def'
out_file.write(text)
assert open('out.txt').read() == ''
open('in.txt', 'w').write('abc def')
with open('in.txt') as in_file:
with open('out.txt', 'w') as out_file:
text = in_file.read()
assert text == 'abc def'
text = text.title()
assert text == 'Abc Def'
out_file.write(text)
assert open('out.txt').read() == ''
assert open('out.txt').read() == 'Abc Def'
from bz2 import BZ2File
from gzip import GzipFile
import os
with GzipFile('1.gz', 'wb') as g, BZ2File('1.bz2', 'wb') as b:
g.write(b'X' * 5000)
b.write(b'X' * 5000)
for ext in ('.gz', '.bz2'):
filename = '1' + ext
print ('The size of the {0} file is {1.st_size} bytes'.format(ext, os.stat(filename)))输出的结果:
The size of the .gz file is 43 bytes
The size of the .bz2 file is 45 bytes
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有