新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:如何實(shí)現(xiàn)Python中字符串格式化輸出?
Python字符串格式化輸出是我們?cè)诓僮髯址畷r(shí)遇到的形式,本文主要介紹Python字符串格式化輸出的四種方法:tuple形式法、字典形式、forma函數(shù)法、 StringIO模塊。下面是具體使用方法。

1、tuple形式法(方便)
print 'hello %s and %s' % ('df', 'another df')
2、字典形式(好用)
print 'hello %(first)s and %(second)s' % {'first': 'df', 'second': 'another df'}
3、forma函數(shù)法(先進(jìn))
print('姓名是 {},年齡是 {}'.format('Tom',20))
輸出:
姓名是:Tom,年齡是:20
4、 StringIO模塊(多個(gè)數(shù)據(jù))
>>> buf = StringIO.StringIO() >>> sprintf(buf, "A = %d, B = %s\n", 3, "foo") >>> sprintf(buf, "C = %d\n", 5) >>> print(buf.getvalue()) A = 3, B = foo C = 5
以上就是字符串格式化輸出的四種方法,大家根據(jù)不同情況可以選擇合適的方法,希望能幫到你哦~
當(dāng)前題目:創(chuàng)新互聯(lián)Python教程:如何實(shí)現(xiàn)Python中字符串格式化輸出?
瀏覽地址:http://www.dlmjj.cn/article/cdgidgc.html


咨詢
建站咨詢
