Python的格式化字符串方法format

python中的字符串格式函数str.format(),{sen}的用法有点类似于关键字参数

{}

>>>print('I\'m {},{}'.format('July','Young'))
I'm July,Young

{0}

>>>print('{1},I\'m {0}, {2}'.format('July','Hello','Young')) #注意顺序
Hello,I'm July, Young

{sen}

>>>print('Great,{sen1},{sen2}'.format(sen1 = 'JULY',sen2 = 'Good work!'))
Great,JULY,Good work!

{0},{sen}混用

>>>print('{0},I\'m {1},{sen}'.format('Hi','fine',sen = 'Let\'s go!'))
Hi,I'm fine,Let's go!

你可能感兴趣的:(Python)