Python開發【第一篇】Python基礎之字串格式化,第一篇python

來源:互聯網
上載者:User

Python開發【第一篇】Python基礎之字串格式化,第一篇python

字串格式化

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

百分比符號的方式相對來說比較老,而format方式則是比較先進的方式,企圖替換古老的方式,目前兩者並存。[PEP-3101]

This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing '%' string formatting operator.

 

1、百分比符號方式

%[(name)][flags][width].[precision]typecode

 

1、順序傳參數
2、指定名稱傳入參數
3、保留小數點後幾位
4、如果出現預留位置,則寫 %%, 列印時 %

  • (name)      可選,用於選擇指定的key 
  • flags          可選,可供選擇的值有:
  • +       靠右對齊;正數前加正好,負數前加負號;
  • -        靠左對齊;正數前無符號,負數前加負號;
  • 空格    靠右對齊;正數前加空格,負數前加負號;
  • 0        靠右對齊;正數前無符號,負數前加負號;用0填充空白處
  • width         可選,佔有寬度
  • .precision   可選,小數點後保留的位元
  • typecode    必選
  • s,擷取傳入對象的__str__方法的傳回值,並將其格式化到指定位置
  • r,擷取傳入對象的__repr__方法的傳回值,並將其格式化到指定位置
  • c,整數:將數字轉換成其unicode對應的值,10進位範圍為 0 <= i <= 1114111(py27則只支援0-255);字元:將字元添加到指定位置
  • o,將整數轉換成 八  進位表示,並將其格式化到指定位置
  • x,將整數轉換成十六進位表示,並將其格式化到指定位置
  • d,將整數、浮點數轉換成 十 進位表示,並將其格式化到指定位置
  • e,將整數、浮點數轉換成科學計數法,並將其格式化到指定位置(小寫e)
  • E,將整數、浮點數轉換成科學計數法,並將其格式化到指定位置(大寫E)
  • f, 將整數、浮點數轉換成浮點數表示,並將其格式化到指定位置(預設保留小數點後6位)
  • F,同上
  • g,自動調整將整數、浮點數轉換成 浮點型或科學計數法表示(超過6位元用科學計數法),並將其格式化到指定位置(如果是科學計數則是e;)
  • G,自動調整將整數、浮點數轉換成 浮點型或科學計數法表示(超過6位元用科學計數法),並將其格式化到指定位置(如果是科學計數則是E;)
  • %,當字串中存在格式化標誌時,需要用 %%表示一個百分比符號

註:Python中百分比符號格式化是不存在自動將整數轉換成二進位表示的方式

 

常用格式化:

tpl = "i am %s" % "alex"tpl = "i am %s age %d" % ("alex", 18) tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18} tpl = "percent %.2f" % 99.97623 tpl = "i am %(pp).2f" % {"pp": 123.425556, } tpl = "i am %.2f %%" % {"pp": 123.425556, }

  

 2、Format方式

format 可以置中、%百分比符號、二進位、數字加逗號、填充。而% 做不到。

[[fill]align][sign][#][0][width][,][.precision][type]
  • fill           【可選】空白處填充的字元
  • align        【可選】對齊(需配合width使用)
  • <,內容靠左對齊
  • >,內容靠右對齊(預設)
  • =,內容靠右對齊,將符號放置在填充字元的左側,且只對數字類型有效。 即使:符號+填充物+數字
  • ^,內容置中
  • sign         【可選】有無符號數字#            【可選】對於二進位、八進位、十六進位,如果加上#,會顯示 0b/0o/0x,否則不顯示
  • +,正號加正,負號加負;
  •  -,正號不變,負號加負;
  • 空格 ,正號空格,負號加負;
  • ,            【可選】為數字添加分隔字元,如:1,000,000
  • width       【可選】格式化位所佔寬度
  • .precision 【可選】小數位保留精度
  • type         【可選】格式化類型
  • 傳入” 字串類型 “的參數
  • s,格式化字串類型資料
  • 空白,未指定類型,則預設是None,同s
  • 傳入“ 整數類型 ”的參數
  • b,將10進位整數自動轉換成2進位表示然後格式化
  • c,將10進位整數自動轉換為其對應的unicode字元
  • d,十進位整數
  • o,將10進位整數自動轉換成8進位表示然後格式化;
  • x,將10進位整數自動轉換成16進位表示然後格式化(小寫x)
  • X,將10進位整數自動轉換成16進位表示然後格式化(大寫X)
  • 傳入“ 浮點型或小數類型 ”的參數
  • e, 轉換為科學計數法(小寫e)表示,然後格式化;
  • E, 轉換為科學計數法(大寫E)表示,然後格式化;
  • f , 轉換為浮點型(預設小數點後保留6位)表示,然後格式化;
  • F, 轉換為浮點型(預設小數點後保留6位)表示,然後格式化;
  • g, 自動在e和f中切換
  • G, 自動在E和F中切換
  • %,顯示百分比(預設顯示小數點後6位)

 常用格式化:

tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')  tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])  tpl = "i am {0}, age {1}, really {0}".format("seven", 18)  tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])  tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)  tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})  tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])  tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)  tpl = "i am {:s}, age {:d}".format(*["seven", 18])  tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)  tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18}) tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15) tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)

重點:

更多格式化操作:https://docs.python.org/3/library/string.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.