重庆教育建设有限公司网站,网站打开速度与服务器,哈尔滨网站小程序制作,青岛商媒做网站怎么样字符串的格式化方法分为两种#xff0c;分别为占位符(%)和format方式。占位符方式在Python2.x中用的比较广泛#xff0c;随着Python3.x的使用越来越广#xff0c;format方式使用的更加广泛。
一 占位符(%)%d
实例(Python3.0)#xff1a;
age 29
print(my age is %d分别为占位符(%)和format方式。占位符方式在Python2.x中用的比较广泛随着Python3.x的使用越来越广format方式使用的更加广泛。
一 占位符(%)%d
实例(Python3.0)
age 29
print(my age is %d %age)
#my age is 29
%s
实例(Python3.0)
name makes
print(my name is %s %name)
#my name is makes
%f
实例(Python3.0)
print(%6.3f % 2.3)
#2.300
print(%f %2.3)
#2.300000
二 format方法
位置映射实例(Python3.0)
print({}:{}.format(192.168.0.100,8888))
#192.168.0.100:8888
关键字映射实例(Python3.0)
print({server}{1}:{0}.format(8888,192.168.1.100,serverWeb Server Info :))
#Web Server Info :192.168.1.100:8888
元素访问实例(Python3.0)
print({0[0]}.{0[1]}.format((baidu,com)))
#baidu.com
填充对齐
^、、分别是居中、左对齐、右对齐实例1(Python3.0)
print({0}*{1}{2:02}.format(3,2,2*3))
#3*206
print({:*^30}.format(centered))
#***********centered***********
实例2(Python3.0)九九乘法表
for i in range(1,10):
a 1
while a i:
print({0}*{1}{2:02}.format(a,i,a*i),end\t)
a 1
print()1*101
1*202 2*204
1*303 2*306 3*309
1*404 2*408 3*412 4*416
1*505 2*510 3*515 4*520 5*525
1*606 2*612 3*618 4*624 5*630 6*636
1*707 2*714 3*721 4*728 5*735 6*742 7*749
1*808 2*816 3*824 4*832 5*840 6*848 7*856 8*864
1*909 2*918 3*927 4*936 5*945 6*954 7*963 8*972 9*981精度设置实例(Python3.0)
print({:.3f}.format(2.1415))
#2.142
print({:.10f}.format(3.1415))
#3.1415000000