【Python学习笔记—01】Python format函数用法详解

it2025-03-13  23

format函数可以接受不限个数的参数,位置可以不按顺序,是通过{}和:来代替%。

format基本用法有很多种,下边分享几种常用语法:

基本用法

print('Hello {0} i am {1}'.format('world','python')) # Hello world i am python print('Hello {} i am {}'.format('world','python')) # Hello world i am python print('{0}, {1}, {2}'.format('a', 'b', 'c')) # a, b, c print('{}, {}, {}'.format('a', 'b', 'c')) # a, b, c pring('{0}, {1}, {0}'.format('a', 'b')) # a, b, a

并且同一个参数可以多次填充,这个是format的优点。

最新回复(0)