python 字符串 format用法

 关键字参数替换

'my name is {name},age is {age}'.format(name='hoho',age=19)

hash = {'name':'hoho','age':18}

'my name is {name},age is {age}'.format(**hash)


位置参数

1 >>> li = ['hoho',18]

2 >>> 'my name is {} ,age {}'.format('hoho',18)

3 'my name is hoho ,age 18'

4 >>> 'my name is {1} ,age {0}'.format(10,'hoho')

5 'my name is hoho ,age 10'

6 >>> 'my name is {1} ,age {0} {1}'.format(10,'hoho')

7 'my name is hoho ,age 10 hoho'

8 >>> 'my name is {} ,age {}'.format(*li)

9 'my name is hoho ,age 18'

参考资料:https://blog.csdn.net/weixin_42181200/article/details/87913169

留言

熱門文章