day7-字符串格式化

msg='i am %s my hobby is %s' % ('lhf','alex')
# #  %代表标识,固定格式  s代表传入的为字符串,该字符串可接受任何类型
# # %d ,只能接收数字
print(msg)
# i am lhf my hobby is alex
msg='i am %s my hobby is %s' % ('lhf',1)
msg='i am %s my hobby is %s' % ('lhf',[1,2])
print(msg)
# i am lhf my hobby is [1, 2]
name='lhf'
age=19
msg='i am %s my hobby is %s' % (name,age)
print(msg)
# i am lhf my hobby is 19
# %f 打印浮点数,默认保留6位。
# %.2f 指.后面保留2位,且四舍五入
tpl = "percent %.2f" % 99.976234444444444444
print(tpl) # percent 99.98
# %.2f %% 打印百分比
tpl = 'percent %.2f %%' % 99.976234444444444444
print(tpl)  # percent 99.98 %
# 打印字典:键值对
tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
print(tpl)  # i am alex age 18
msg='i am %(name)+60s my hobby is alex' %{'name':'lhf'}
print(msg) # i am                                                          lhf my hobby is alex
# 以\033开头结尾,加颜色
msg='i am \033[43;1m%(name)+60s\033[0m my hobby is alex' %{'name':'lhf'}
print(msg) # i am                                                          lhf my hobby is alex
print('root','x','0','0',sep=':') # root:x:0:0
# print('root'+':'+'x'+':'+'0','0')

猜你喜欢

转载自www.cnblogs.com/mada1027/p/11688634.html
今日推荐