for student in students: print('%(name)s is %(age)d years old' % student)
输出结果如下所示:
1 2 3 4 5 6 7 8 9 10 11 12
1234 => hex => 4d2 1234 => hex => 4D2 1234 => hex => 0x4d2 1234 => hex => 0X4D2 value f is 3.141593 value f is 3.1416 name: Han Meimei, age: 17 name: Li Lei , age: 18 name: Li Lei, age: 0000000018 Han Meimei is 17 years old Li Lei is 18 years old Wei Hua is 17 years old
字符串模板
示例如下:
1 2 3 4 5 6 7
from string import Template
s = Template('Hi, $name! $name is $age years old.') print(s.substitute(name='Han Meimei', age='17'))
print('{0} is {1} years old.'.format('lily', 18)) print('{} is {} years old.'.format('lily', 18)) print('Hi {0}. {0} is {1} years old.'.format('lily', 18))
# 关键字参数 print('{name} is {age} years old'.format(name='lily', age=18))
# 下标参数 lily = ['lily', 18] print('{0[0]} is {0[1]} years old'.format(lily))
lily is 18 years old. lily is 18 years old. Hi lily. lily is 18 years old. lily is 18 years old lily is 18 years old 3.14 3.14 3.14 00003.14 aaaa3.14 3.1416 00003.1416 1100 12 14 c 0xc 0XC 1,230,000,000