新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python字符串方法format()如何使用
1、格式字符串中用花括號表示待插入值的位置、索引名稱和格式,并在format方法參數(shù)中寫出待插入值。

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比愛輝網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式愛輝網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋愛輝地區(qū)。費用合理售后完善,十多年實體公司更值得信賴。
2、轉(zhuǎn)換標志:跟隨感嘆號后的單詞表示以相應的格式轉(zhuǎn)換給定的值。
格式說明符:跟隨冒號后的表現(xiàn),用于詳細指定字符串的格式。
實例
>>> # 指定表示方式
>>> print('in decimal: {0:d}\nin binary : {0:b}'.format(10))
# in decimal: 10
# in binary : 1010
>>> print('in fixed-point notation: {0:f}\nin scientific notation: {0:e}'.format(0.25))
# in fixed-point notation: 0.250000
# in scientific notation: 2.500000e-01
>>> # 指定精度
>>> print("{0:.2f}".format(1/3))
# 0.33
>>> # 指定寬度,默認右對齊
>>> print("{0:10.2f}".format(1/4))
# 0.25
>>> # 指定對齊方式,默認用空格填充
>>> print("{0:<10.2f}\n{0:^10.2f}\n{0:>10.2f}".format(1/6))
# 0.17
# 0.17
# 0.17
>>> # 指定對齊方式,并指定填充字符
>>> print("{0:*<10.2f}\n{0:*^10.2f}\n{0:*>10.2f}".format(1/7))
# 0.14******
# ***0.14***
# ******0.14以上就是Python字符串方法format()的使用,希望對大家有所幫助。更多Python學習指路:創(chuàng)新互聯(lián)Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
新聞標題:創(chuàng)新互聯(lián)Python教程:python字符串方法format()如何使用
路徑分享:http://m.fisionsoft.com.cn/article/cddsehc.html


咨詢
建站咨詢
