91aaa在线国内观看,亚洲AV午夜福利精品一区二区,久久偷拍人视频,久久播这里有免费视播

<strong id="fvuar"></strong>

  • <sub id="fvuar"><dl id="fvuar"><em id="fvuar"></em></dl></sub>

    1. 千鋒教育-做有情懷、有良心、有品質的職業(yè)教育機構

      手機站
      千鋒教育

      千鋒學習站 | 隨時隨地免費學

      千鋒教育

      掃一掃進入千鋒手機站

      領取全套視頻
      千鋒教育

      關注千鋒學習站小程序
      隨時隨地免費學習課程

      當前位置:首頁  >  技術干貨  > pythonformat怎么用

      pythonformat怎么用

      來源:千鋒教育
      發(fā)布人:xqq
      時間: 2023-11-11 07:37:53 1699659473

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

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

      1、基本用法

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

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

      (3)帶關鍵字,即“{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'))#帶關鍵字

      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ù)轉換成對應的Unicode字符串。

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

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

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

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

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

      'n'-數(shù)字。當值為整數(shù)時和'd'相同,值為浮點數(shù)時和'g'相同。不同的是它會根據(jù)區(qū)域設置插入數(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>>>

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

      tags: python培訓
      聲明:本站稿件版權均屬千鋒教育所有,未經(jīng)許可不得擅自轉載。
      10年以上業(yè)內強師集結,手把手帶你蛻變精英
      請您保持通訊暢通,專屬學習老師24小時內將與您1V1溝通
      免費領取
      今日已有369人領取成功
      劉同學 138****2860 剛剛成功領取
      王同學 131****2015 剛剛成功領取
      張同學 133****4652 剛剛成功領取
      李同學 135****8607 剛剛成功領取
      楊同學 132****5667 剛剛成功領取
      岳同學 134****6652 剛剛成功領取
      梁同學 157****2950 剛剛成功領取
      劉同學 189****1015 剛剛成功領取
      張同學 155****4678 剛剛成功領取
      鄒同學 139****2907 剛剛成功領取
      董同學 138****2867 剛剛成功領取
      周同學 136****3602 剛剛成功領取
      相關推薦HOT