python 字符串模板功能,python字符串模板,# method 1te


# method 1template = "hello %s , your message is %s " % ("apache","tomcat")print(template)#method 2template = "hello %(name)s , your message is %(message)s" %{"name":"apache","message":"tomcat"}print(template)#method3template = "hello {0} , your message is {1} ".format("apache","tomcat")print(template)#method4template = "hello {name} , your message is {message} ".format(name="apache",message="tomcat")print(template)#method5name = "apache"message = "tomcat"print(vars())templdate = "hello {name} , your message is {message} " % (vars())print(template)print("slide".center(20,"-"))#method6tempList = list("list")argument = tempList[0],tempList[-1],tempList[1:3]print('first={0},last = {1},middle = {2}' .format(*argument))#method7print("what {1:10} world ! {0:10} a".format("apache","beauty"))print("what {1:>10} world ! {0:<10} a".format("apache","beauty"))#method8import sysprint("{0.platform:>10} = {1:<10}" .format(sys,"laptop"))print("{0.platform:>10} = {1[item]:<10}".format(sys,dict(item="laptop")))#method9import stringtempTemplate = string.Template("Hello $name ,your message is $message")#print(locals())print(tempTemplate.substitute(locals()))print(tempTemplate.substitute(vars()))#method10print("{0:-10.3f},{1:010.1f}".format(1.12312312,1.12312312))#method11print("{0:X},{1:o},{2:b}".format(16,16,16))#method12print(format(1.12412412,".2f"))#该片段来自于http://byrx.net

评论关闭