资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

字符串格式化

str_1 = '字符串的格式化'
v11 = str_1.center(100,'-')
print(v11)
name = "huangyoujian"
age = 23
print("my name is %s,age is %d"%(name,age))

创新互联是一家专业提供镇坪企业网站建设,专注与成都网站设计、成都做网站、H5页面制作、小程序制作等业务。10年已为镇坪众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。

print("my name is %(name)s,age is %(age)d"%({'name':'haha','age':24}))

print("my name is %(name)-60s,age is %(age)d"%({'name':"laowang",'age':24}))# 左对齐
print("my name is %(name)+60s,age is %(age)d"%({'name':"laowang",'age':24}))# 右对齐

print('root','X','0','0',sep=':')
"""format字符串格式化"""
print(''50)
v = "my name is {},age is {},hobby is {}".format("huangyoujan",18,'playgame')
print(v)
print(''50)
print("可以根据索引来取值")
v1 = "my name is {2},age is {1},hobby is {0}".format("huangyoujan",18,'playgame')
print(v1)
print(''50)
print('也可以只取其中的一个值')
v2 = "my name is {1},age is {1},hobby is {1}".format("huangyoujan",18,'playgame')
print(v2)
print(''50)
print('可以传递一个列表')
v3 = "my name is {0},age is {1}".format(['laohuang',18])
print(v3)
print('
'50)
print('也可以传递一个表达式')
v4 = "my name is {name},age is {age},hobby is {name}".format(name='laowang',age=24)
print(v4)
print('
'*50)
print('还可以传递一个字典')
v5 = "my name is {name},age is {age},hobby is {name}".format({'name':'HYJ','age':18})
print(v5)
print(''50)
print('根据索引取传递列表中的值')
v6 = "my name is {0[0]},age is {0[1]},hobby is {0[2]}".format([0,1,2],[3,4])
print(v6)
print(''50)
print('按指定格式传递参数')
v7 = "my name is {:s},age is {:d},hobby is {:s}".format("huangyoujan",18,'playgame')
print(v7)
print(''50)
v8 = "my name is {name:s},age is {age:d},hobby is {hobby:s}".format(name="huangyoujan",age=18,hobby='playgame')
print(v8)
print(''50)
print('按指定格式传递一个字典')
v9 = "my name is {name:s},age is {age:d},hobby is {hobby:s}".format(
{'name':'laowang','age':18,'hobby':'playgame'})
print(v9)
print(''50)
print('进制之间的转换')
v10 = "number:{:b},{:o},{:d},{:x},{:X},{:%}".format(15,15,15,15,15,18.54121)
print(v10)
print(''50)
v13 = "number:{0:b},{0:o},{0:d},{0:x},{0:X},{0:%}".format(15)
print(v13)

str_2 = 'END'
v12 = str_2.center(100,'-')
print(v12)


网站题目:字符串格式化
链接地址:http://cdkjz.cn/article/pphocs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220