字符串格式化(七)-format

print("i am %s" %'admin')   # i am admin
msg = "i am %s" %'Alex'
print(msg)  # i am Alex
print("i am %s, i am %d" %('admin', 18))    # i am admin, i am 18
tp1 = 'i am %(name)s age %(age)d' % {'name':'alex', 'age':18}
print(tp1)  # i am alex age 18
# 加颜色 \033[43;1m........\033[0m
print('\033[45;1mhello\033[0m')

print('root', 'x', '0', '0', sep=':')   # root:x:0:0

# format
tp2 = 'i am {}, age {}, {}'.format('alex', 28, 'hhh')
print(tp2)  # i am alex, age 28, hhh

 

print("i am %s" %'admin')   # i am admin
msg = "i am %s" %'Alex'
print(msg)  # i am Alex
print("i am %s, i am %d" %('admin', 18))    # i am admin, i am 18
tp1 = 'i am %(name)s age %(age)d' % {'name':'alex', 'age':18}
print(tp1)  # i am alex age 18
# 加颜色 \033[43;1m........\033[0m
print('\033[45;1mhello\033[0m')

print('root', 'x', '0', '0', sep=':')   # root:x:0:0

# format
tp2 = 'i am {}, age {}, {}'.format('alex', 28, 'hhh')
print(tp2)  # i am alex, age 28, hhh

tp3 = 'i am {2}, age {0}, {1}'.format('alex', 28, 'hhh')
print(tp3)  # i am hhh, age alex, 28

tp4 = 'i am {name}, age {age}'.format(name = 'alex', age = 28)
print(tp4)  # i am alex, age 28

tp5 = 'i am {name}, age {age}'.format(**{'name' : 'alex', 'age' : 28})
print(tp5)  # i am alex, age 28

tp6 = 'i am {0[0]}, age {1[1]}'.format([1, 2, 3], [6, 7, 8])
print(tp6) # i am 1, age 7

tp7 = 'i am {:s}, age {:d}'.format('alex', 18)
print(tp7)  # i am alex, age 18

tp8 = 'i am {:s}, age {:d}'.format(*['alex', 18])
print(tp8)  # i am alex, age 18

tp8 = 'num: {:b},{:o},{:d},{:x},{:%}'.format(9, 9, 10, 10, 3)
print(tp8)  # num: 1001,11,10,a,300.000000%

 

print(r'\tpython')
# r-->raw 字符串前面加r 表示原样输出字符串,不输出转义字符
# \\t也乐意原样输出

 

''' ''' 三引号
1,可以保证字符串的原样输出
2,也可以作为注释使用
print(''' hello world, 你好, 世界 ''')#不需要加换行符\n,'''可以原样输出所包含的字符串

 

posted @ 2019-02-13 23:55  狂奔~  阅读(214)  评论(0编辑  收藏  举报