Python基础 之 格式化字符

1. 比较简单容易理解的字符串格式化

>>> print("%d%% people like sleeping." %90)
90% people like sleeping.

2. 比较好用的字典类型:字符串格式化

>>> print('customerA: %(first)s and customerB: %(second)s' % {'first': 'Jack', 'second': 'Kate'})
customerA: Jack and customerB: Kate

3. 比较高级的字符串格式化方法format

>>> print('{name} is {age} years old.'.format(name='Kate', age='20') )
Kate is 20 years old.

>>> print('{0} is {1} years old.'.format('Kate', 20) )
Kate is 20 years old.

相关文章: