#输出平方和立方的两种方法 for i in range(1,11): print(repr(i).rjust(2),repr(i*i).rjust(3),end=' ')#第一大列和第二大列所占空格分别为两个和三个(每一大列容纳酔大的空格数) print(repr(i*i*i).rjust(4)) #第三大列所占空格为四个
for x in range(1,11): print('{0:2d},{1:3d},{2:4d}'.format(x,x*x,x*x*x))#每列的空格由print()增加的 #repr.rjust()方法表示向右对齐
#str.zfill()表示向左填充0,如下 a = '12'.zfill(5) #只有两位,不足5位,需要在前面填充0 print(a) b = '-3.14'.zfill(7) #所有字符有5位,不足7位,需要在前面填充0 print(b) c = '3.14159265359'.zfill(5)#由于字符串位数比给定的大,不需要填充 print(c) d = '3.1416'.zfill(5) print(d) #当字符串中的字符数少于.zfill中的数字,则在前面填充0;当等于或大于时,正常输出字符串。
#.format()函数用法,format输出格式其实就是str.format()形式 print('we are the {},who say "{}"!'.format('knight','Ni')) print('{0} and {1}.'.format('spam','eggs'))#{}中数字表示所在位置 print('{1} and {0}.'.format('spam','eggs'))#{0}表示.format中的第二个参数在输出参数中的第一个位置
#关键字参数用在format中 print('this {food} is {adjective}.'.format(food='spam',adjective='absolutely horrible'))#输出关键字参数对应的键值
#位置与关键字参数组合 print('the story is {0},{1} and {other}.'.format('Bill','Manfred',other='Georg'))
#!a(apply acsii),!r(apply repr),!s(apply str).就是将输入的转换为对应的格式 contents = 'eels' print('my hovercraft is full of {}.'.format(contents)) print('my hovercraft is full of {!r}!'.format(contents))#转换为repr格式 print('my hovercraft is full of {!a}.'.format(contents))#转换为ascii格式 print('my hovercraft is full of {!s}.'.format(contents))#转换为str格式
#用‘':',':'之后的整数表示字符所需要的最小范围 table = {'Sjoerd':4127,'Jack':4098,'Dcab':7679} for name,phone in table.items(): #table.items表示关键字参数 print('{0:10} ==> {1:10d}'.format(name,phone)) #第一个表示输出的是字符,第二个表示输出的是数字,通过位置表示键值
Hello world. 'Hello world.' 0.14285714285714285 the value of x is32.5 ,and y is40000... Hello , world.
'Hello , world.\n' (32.5, 40000, ('spam', 'eggs')) 111 248 3927 41664 525125 636216 749343 864512 981729 101001000 1, 1, 1 2, 4, 8 3, 9, 27 4, 16, 64 5, 25, 125 6, 36, 216 7, 49, 343 8, 64, 512 9, 81, 729 10,100,1000 00012 -003.14 3.14159265359 3.1416 we are the knight,who say "Ni"! spam and eggs. eggs and spam. this spam is absolutely horrible. the story is Bill,Manfred and Georg. my hovercraft is full of eels. my hovercraft is full of 'eels'! my hovercraft is full of 'eels'. my hovercraft is full of eels. Jack ==> 4098 Dcab ==> 7679 Sjoerd ==> 4127 Jack:4098; Sjoerd:4127; Dcab:6079493 Jack:4098; Sjoerd:4127; Jack:4098; Sjoerd:4127; Dcab:6079493 the value of PI is approximately 3.142.