• <strike id="6sogq"><s id="6sogq"></s></strike>
  • <strike id="6sogq"></strike>

    千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機構(gòu)

    400-811-9990
    手機站
    千鋒教育

    千鋒學(xué)習(xí)站 | 隨時隨地免費學(xué)

    千鋒教育

    掃一掃進入千鋒手機站

    領(lǐng)取全套視頻
    千鋒教育

    關(guān)注千鋒學(xué)習(xí)站小程序
    隨時隨地免費學(xué)習(xí)課程

    上海
    • 北京
    • 鄭州
    • 武漢
    • 成都
    • 西安
    • 沈陽
    • 廣州
    • 南京
    • 深圳
    • 大連
    • 青島
    • 杭州
    • 重慶
    當前位置:成都千鋒IT培訓(xùn)  >  技術(shù)干貨  >  pythonformat怎么用

    pythonformat怎么用

    來源:千鋒教育
    發(fā)布人:xqq
    時間: 2023-11-08 12:21:52

    python的format用法相對基本格式化輸出采用‘%’的方法,format()功能更強大,該函數(shù)把字符串當成一個模板,通過傳入的參數(shù)進行格式化,并且使用大括號‘{}’作為特殊字符代替‘%’

    使用方法由兩種:b.format(a)和format(a,b)。

    1、基本用法

    (1)不帶編號,即“{}”

    (2)帶數(shù)字編號,可調(diào)換順序,即“{1}”、“{2}”

    (3)帶關(guān)鍵字,即“{a}”、“{tom}”

    1>>>print('{}{}'.format('hello','world'))#不帶字段

    2helloworld

    3>>>print('{0}{1}'.format('hello','world'))#帶數(shù)字編號

    4helloworld

    5>>>print('{0}{1}{0}'.format('hello','world'))#打亂順序

    6helloworldhello

    7>>>print('{1}{1}{0}'.format('hello','world'))

    8worldworldhello

    9>>>print('{a}{tom}{a}'.format(tom='hello',a='world'))#帶關(guān)鍵字

    10worldhelloworld

    2、進階用法

    (1)<(默認)左對齊、>右對齊、^中間對齊、=(只用于數(shù)字)在小數(shù)點后進行補齊

    (2)取位數(shù)“{:4s}”、"{:.2f}"等

    1>>>print('{}and{}'.format('hello','world'))#默認左對齊

    2helloandworld

    3>>>print('{:10s}and{:>10s}'.format('hello','world'))#取10位左對齊,取10位右對齊

    4helloandworld

    5>>>print('{:^10s}and{:^10s}'.format('hello','world'))#取10位中間對齊

    6helloandworld

    7>>>print('{}is{:.2f}'.format(1.123,1.123))#取2位小數(shù)

    81.123is1.12

    9>>>print('{0}is{0:>10.2f}'.format(1.123))#取2位小數(shù),右對齊,取10位

    101.123is1.12

    3、多個格式化

    'b'-二進制。將數(shù)字以2為基數(shù)進行輸出。

    'c'-字符。在打印之前將整數(shù)轉(zhuǎn)換成對應(yīng)的Unicode字符串。

    'd'-十進制整數(shù)。將數(shù)字以10為基數(shù)進行輸出。

    'o'-八進制。將數(shù)字以8為基數(shù)進行輸出。

    'x'-十六進制。將數(shù)字以16為基數(shù)進行輸出,9以上的位數(shù)用小寫字母。

    'e'-冪符號。用科學(xué)計數(shù)法打印數(shù)字。用'e'表示冪。

    'g'-一般格式。將數(shù)值以fixed-point格式輸出。當數(shù)值特別大的時候,用冪形式打印。

    'n'-數(shù)字。當值為整數(shù)時和'd'相同,值為浮點數(shù)時和'g'相同。不同的是它會根據(jù)區(qū)域設(shè)置插入數(shù)字分隔符。

    '%'-百分數(shù)。將數(shù)值乘以100然后以fixed-point('f')格式打印,值后面會有一個百分號。

    1>>>print('{0:b}'.format(3))

    211

    3>>>print('{:c}'.format(20))

    4

    5>>>print('{:d}'.format(20))

    620

    7>>>print('{:o}'.format(20))

    824

    9>>>print('{:x}'.format(20))

    1014

    11>>>print('{:e}'.format(20))

    122.000000e+01

    13>>>print('{:g}'.format(20.1))

    1420.1

    15>>>print('{:f}'.format(20))

    1620.000000

    17>>>print('{:n}'.format(20))

    1820

    19>>>print('{:%}'.format(20))

    202000.000000%

    21>>>

    以上內(nèi)容為大家介紹了python培訓(xùn)之format怎么用,希望對大家有所幫助,如果想要了解更多Python相關(guān)知識,請關(guān)注IT培訓(xùn)機構(gòu):千鋒教育。

    聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。

    猜你喜歡LIKE

    python delattr函數(shù)如何使用?

    2023-11-10

    python time.strptime的格式化

    2023-11-10

    pythonGIL在Python多線程的應(yīng)用

    2023-11-10

    最新文章NEW

    python中pdb模塊怎么用?

    2023-11-10

    Python如何截圖保存?

    2023-11-10

    python?中缺少module怎么辦?

    2023-11-10

    相關(guān)推薦HOT

    更多>>

    快速通道 更多>>

    最新開班信息 更多>>

    網(wǎng)友熱搜 更多>>