首页 > 编程 > Python > 正文

python字符串格式化

2019-11-06 07:06:50
字体:
来源:转载
供稿:网友
>>> "{key}={value}".format(key="a", value=10) # 使⽤命名参数'a=10'>>> "[{0:<10}], [{0:^10}], [{0:*>10}]".format("a") # 左中右对⻬'[a ], [ a ], [*********a]'>>> "{0.platform}".format(sys) # 成员'win32'>>> "{0[a]}".format(dict(a=1, b=2)) # 字典'1'>>> "{0[3]}".format(range(10)) # 列表'5'>>> "My name is {0} :-{{}}".format('Mongo') # 真得想显示{},需要双{}'My name is Mongo :-{}'>>> "{0!r:20}".format("Hello")"'Hello' ">>> "{0!s:20}".format("Hello")'Hello '>>> "Today is: {0:%a %b %d %H:%M:%S %Y}".format(datetime.today())'Today is: Sun Mar 05 18:35:22 2017'
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表