python開(kāi)發(fā)之str.format()用法實(shí)例分析
本文實(shí)例分析了python開(kāi)發(fā)之str.format()用法。分享給大家供大家參考,具體如下:
格式化一個(gè)字符串的輸出結(jié)果,我們?cè)诤芏嗟胤蕉伎梢钥吹剑纾篶/c++中都有見(jiàn)過(guò)
下面看看python中的字符串格式函數(shù)str.format():
#使用str.format()函數(shù)
#使用'{}'占位符
print('I\'m {},{}'.format('Hongten','Welcome to my space!'))
print('#' * 40)
#也可以使用'{0}','{1}'形式的占位符
print('{0},I\'m {1},my E-mail is {2}'.format('Hello','Hongten','hongtenzone@foxmail.com'))
#可以改變占位符的位置
print('{1},I\'m {0},my E-mail is {2}'.format('Hongten','Hello','hongtenzone@foxmail.com'))
print('#' * 40)
#使用'{name}'形式的占位符
print('Hi,{name},{message}'.format(name = 'Tom',message = 'How old are you?'))
print('#' * 40)
#混合使用'{0}','{name}'形式
print('{0},I\'m {1},{message}'.format('Hello','Hongten',message = 'This is a test message!'))
print('#' * 40)
#下面進(jìn)行格式控制
import math
print('The value of PI is approximately {}.'.format(math.pi))
print('The value of PI is approximately {!r}.'.format(math.pi))
print('The value of PI is approximately {0:.3f}.'.format(math.pi))
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
for name, phone in table.items():
print('{0:10} ==> {1:10d}'.format(name, phone))
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; ''Dcab: {0[Dcab]:d}'.format(table))
運(yùn)行效果:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> I'm Hongten,Welcome to my space! ######################################## Hello,I'm Hongten,my E-mail is hongtenzone@foxmail.com Hello,I'm Hongten,my E-mail is hongtenzone@foxmail.com ######################################## Hi,Tom,How old are you? ######################################## Hello,I'm Hongten,This is a test message! ######################################## The value of PI is approximately 3.141592653589793. The value of PI is approximately 3.141592653589793. The value of PI is approximately 3.142. Jack ==> 4098 Sjoerd ==> 4127 Dcab ==> 7678 Jack: 4098; Sjoerd: 4127; Dcab: 8637678 >>>
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python基于動(dòng)態(tài)規(guī)劃算法解決01背包問(wèn)題實(shí)例
這篇文章主要介紹了Python基于動(dòng)態(tài)規(guī)劃算法解決01背包問(wèn)題,結(jié)合實(shí)例形式分析了Python動(dòng)態(tài)規(guī)劃算法解決01背包問(wèn)題的原理與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-12-12
python實(shí)現(xiàn)word 2007文檔轉(zhuǎn)換為pdf文件
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)word 2007文檔轉(zhuǎn)換為pdf文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Python基于opencv的圖像壓縮算法實(shí)例分析
這篇文章主要介紹了Python基于opencv的圖像壓縮算法,結(jié)合實(shí)例形式分析了使用opencv進(jìn)行圖像壓縮的常用操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-05-05
Python3實(shí)現(xiàn)二叉樹(shù)的最大深度
這篇文章主要介紹了Python3實(shí)現(xiàn)二叉樹(shù)的最大深度, 文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Python模塊相關(guān)知識(shí)點(diǎn)小結(jié)
這篇文章主要介紹了Python模塊相關(guān)知識(shí)點(diǎn),總結(jié)分析了Python模塊的功能、原理、使用方法與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03
變長(zhǎng)雙向rnn的正確使用姿勢(shì)教學(xué)
這篇文章主要介紹了變長(zhǎng)雙向rnn的正確使用姿勢(shì),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
python中print的不換行即時(shí)輸出的快速解決方法
下面小編就為大家?guī)?lái)一篇python中print的不換行即時(shí)輸出的快速解決方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考2016-07-07
離線狀態(tài)下在jupyter notebook中使用plotly實(shí)例
這篇文章主要介紹了離線狀態(tài)下在jupyter notebook中使用plotly實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04

