字串格式化,

來源:互聯網
上載者:User

字串格式化,

python字串格式化兩種方式:百分比符號方式、format方式

用百分比符號字串拼接

s = "i am %s , and i am %s" % ("an assassin", "a killer")print(s)s = "i am %s , age %d" % ("an assassin", 19)# %d 只能拼接數字print(s)#輸出結果:i am an assassin , and i am a killeri am an assassin , age 19

 

列印浮點數:

#列印浮點數s1 = "percent % f" % 99.62375187  #小數點後預設是六位s2 = "percent % .2f" % 99.587627138678  #可以修改幾位小數print(s1)print(s2)#輸出結果:percent  99.623752percent  99.59

 

列印百分比:

#列印百分百比s = "percent % .2f %%" % 99.62375187print(s)#輸出結果:percent  99.62 %

 

 

format

s = "i am {},age {},{}".format("reese", 20, "assassin")print(s)s = "i am {2},age {0},{1}".format("reese", 20, "assassin")print(s)#輸出結果:i am reese,age 20,assassini am assassin,age reese,20

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.