字符串格式化%s和format

通过位置
time1 = '{}--{}--{} {}::{}::{}'.format(2018,11,'02',11,43,50)
print(time1)
time1 = '{4}--{5}--{3} {3}::{1}::{0}'.format(2018,11,'02',11,43,50)  #format 可以添加索引
print(time1)
# 输出
2018--11--02 11::43::50
43--50--11 11::11::2018
列表传值 *args
list1 = ['can add post','can delete post','can ban user']
print('管理员的权限为: “{}”、“{}”、“{}”'.format(*list1))
#  一定要加 *  
# 结果: 管理员的权限为: “can add post”、“can delete post”、“can ban user”
通过关键字
print('{名字}今天{动作}'.format(名字='陈某某',动作='拍视频'))#通过关键字
grade = {'name' : '陈某某', 'fenshu': '59'}
print('{name}电工考了{fenshu}'.format(**grade))#通过关键字,可用字典当关键字传入值时,在字典前加**即可
# 输出
陈某某今天拍视频
陈某某电工考了59
列表不能用 % s 格式化内部元素
list1 = ['can add post', 'can delete post', 'can ban user']

# 两种情况均会报错。可以用str.format的方式
# print('管理员的权限为: %()s %()s %()s' % (list1))
# print('管理员的权限为: %s %s %s' % (*list1))
字典可以用 % s格式化内部元素(key必须是字符串类型),不可用 ** dict的方式
dict1 = {'1': 'can add post',
         '2': 'can delete post',
         '3': 'can ban user',
         }
print('管理员的权限为: %(1)s %(2)s %(3)s' % dict1)  # 管理员的权限为: can add post can delete post can ban user
print('管理员的权限为: %(1)s %(1)s %(3)s' % dict1)  # 管理员的权限为: can add post can add post can ban user
name,age = '张松会死',90
dict1 = {'name':'张松会死','age':20,}
print('%s的年龄是%d' %(name,age))
print('%s的年龄是%s' %(dict1['name'],dict1['age']))
print('%s的年龄是%f' %(name,age))   #默认6位小数
print('%s的年龄是%.f' %(name,age))           #没有小数
print('%s的年龄是%.2f' %(name,age))          #两位小数
print('%20s的年龄是%.f' %(name,age))         # 空出20个字符
print('%.3s的年龄是%.f' %(name,age))          #取几个字符(注意中文字符与字节的区别)
print('%(name)s的年龄是%(age).f' %dict1)     #   利用  字典 的 key  进行 字典 传值
#输出
管理员的权限为: can add post can delete post can ban user
管理员的权限为: can add post can add post can ban user
张松会死的年龄是90
张松会死的年龄是20
张松会死的年龄是90.000000
张松会死的年龄是90
张松会死的年龄是90.00
                张松会死的年龄是90
张松会的年龄是90
张松会死的年龄是20

%s的格式化

string = "hello"
# %s打印时结果是hello
print("string01=%s" % string )
# output: string=hello
# %2s意思是字符串长度为2,当原字符串的长度超过2时,按原长度打印,所以%2s的打印结果还是hello
a='d'
print("string02=%2s" % a )
# output: string02= d

# %7s意思是字符串长度为7,当原字符串的长度小于7时,在原字符串左侧补空格,
# 所以%7s的打印结果是  hello
print("string03=%7s" % string)    # output: string=  hello
print("string03=%3s" % string)   # 输出: string03=hello

# %-7s意思是字符串长度为7,当原字符串的长度小于7时,在原字符串右侧补空格,
# 所以%-7s的打印结果是  hello
print("string04=%-7s!" % string)      # output: string=hello  !

# %.2s意思是截取字符串的前2个字符,所以%.2s的打印结果是he
print("string05=%.2s" % string)
  # output: string=he

# %.7s意思是截取字符串的前7个字符,当原字符串长度小于7时,即是字符串本身,
# 所以%.7s的打印结果是hello
print("string06=%.7s" % string)
  # output: string=hello

# %a.bs这种格式是上面两种格式的综合,首先根据小数点后面的数b截取字符串,
# 当截取的字符串长度小于a时,还需要在其左侧补空格
print("string07=%7.2s" % string)
  # output: string=     he
print("string08=%2.7s" % string )
 # output: string=hello
print("string09=%10.7s" % string )
 # output: string=     hello

# 还可以用%*.*s来表示精度,两个*的值分别在后面小括号的前两位数值指定
print("string10=%*.*s" % (7, 2, string))
  # output: string=     he

通过映射 list

a_list = ['chuhao',20,'china']
print( 'my name is {0[0]},from {0[2]},age is {0[1]}'.format(a_list))
# my name is chuhao,from china,age is 20

通过映射 dict

b_dict = {'name':'chuhao','age':20,'province':'shanxi'}
print( 'my name is {name}, age is {age},from {province}'.format(**b_dict))
# my name is chuhao, age is 20,from shanxi

填充与对齐

print ('{:>8}'.format('189'))
print ('{:0>8}'.format('189'))
print( '{:a>9}'.format('189'))
print( '{:a>7}'.format('123456789'))

精度与类型f #保留4位小数

print( '{:.4f}'.format(321.33345))
# 321.33

用来做金额的千位分隔符

print( '{:,}'.format(1234567890))
# 1,234,567,890

其他类型 主要就是进制了,b、d、o、x分别是二进制、十进制、八进制、十六进制。

print ('{:b}'.format(180)) #二进制 10010)
print ('{:d}'.format(180)) #十进制 18
print( '{:o}'.format(180)) #八进制 22
print ('{:x}'.format(180)) #十六进制12
posted @ 2021-04-15 22:25  ty1539  阅读(518)  评论(0编辑  收藏  举报