- # method 1
- template = "hello %s , your message is %s " % ("apache","tomcat")
- print(template)
- #method 2
- template = "hello %(name)s , your message is %(message)s" %{"name":"apache","message":"tomcat"}
- print(template)
- #method3
- template = "hello {0} , your message is {1} ".format("apache","tomcat")
- print(template)
- #method4
- template = "hello {name} , your message is {message} ".format(name="apache",message="tomcat")
- print(template)
- #method5
- name = "apache"
- message = "tomcat"
- print(vars())
- templdate = "hello {name} , your message is {message} " % (vars())
- print(template)
- print("slide".center(20,"-"))
- #method6
- tempList = list("list")
- argument = tempList[0],tempList[-1],tempList[1:3]
- print('first={0},last = {1},middle = {2}' .format(*argument))
- #method7
- print("what {1:10} world ! {0:10} a".format("apache","beauty"))
- print("what {1:>10} world ! {0:<10} a".format("apache","beauty"))
- #method8
- import sys
- print("{0.platform:>10} = {1:<10}" .format(sys,"laptop"))
- print("{0.platform:>10} = {1[item]:<10}".format(sys,dict(item="laptop")))
- #method9
- import string
- tempTemplate = string.Template("Hello $name ,your message is $message")
- #print(locals())
- print(tempTemplate.substitute(locals()))
- print(tempTemplate.substitute(vars()))
- #method10
- print("{0:-10.3f},{1:010.1f}".format(1.12312312,1.12312312))
- #method11
- print("{0:X},{1:o},{2:b}".format(16,16,16))
- #method12
- print(format(1.12412412,".2f"))
- #该片段来自于http://www.codesnippet.cn/detail/171020136456.html
来源: http://www.codesnippet.cn/detail/171020136456.html