python字符串功能_python 字符串模板功能

1.[代码][Python]代码

'''

Created on 2013-6-14

@author: Administrator

'''

# 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"))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值