Python格式化字符串format
li = ['hoho', 19]
tup = ('haha', 20)
dic1 = {'name': 'tom', 'age': 38}
set1 = {'yoy0', 21}
str1 = 'my name is {}, age {}'
def first():
print(str1.format('tom', 18))
print(str1.format(*li))
print(str1.format(*tup))
print(str1.format(*set1))
print(str1.format(*dic1))
str2 = 'my name is {name}, age {age}'
def second():
dic2 = {'name': 'lily', 'age': 38}
print(str2.format(name='hoho', age=19))
print(str2.format(**dic2))
def third_index():
str3 = 'my name is {1}, age {0},sex {sex}'
print(str3.format(18, 'tom', sex='男'))
def four_format():
str3 = '{:a>10}'
print(str3.format(10))
print('{:a<10}'.format(10))
print('{:a^10}'.format(10))
def five_jindu():
str5 = 'my name is {0}, age {1:0>2},身高{2:.2f}米,体重{3:.2f}'
print(str5.format('tom', 9, 183.3122, 160.1233))
four_format()