這篇文章主要介紹了Python字串格式化的方法(兩種) ,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟著小編過來看看吧
本文介紹了Python字串格式化,主要有兩種方法,分享給大家,具體如下
用於字串的拼接,效能更優。
字串格式化有兩種方式:百分號方式、format方式。
百分號方式比較老,而format方式是比較先進的,企圖替代古老的方式,目前兩者共存。
1、百分號方式
格式:%[(name)][flags][width].[precision]typecode
(name) 可選,用於選擇指定的key
flags 可選,可選擇的數值為:
- ##+ 右對齊:正數的加正號,負數的加負號
- - 左對齊:正數前沒有負號,負數前加負號
- width 可選,佔有寬度
- .precision 可選,小數點後保留的位數
- #typecode 必選
- s,取得傳入的物件__str__方法的回傳值,並格式化至指定位置
- #r,取得傳入物件的__repr__方法的回傳值,並將其格式化至指定位置
- c,整數:將數字轉換成其unicode對應的值,10進位範圍為0 o,將整數轉換成八進位表示,並將其格式化到指定位置
- x,將整數轉換成16進制,並將其格式化到指定位置
-
d,將整數,浮點數轉化為十進位表示,並將其格式化到指定位置
#>>> s = 'i am %s,age %d' %('cai',18)
>>> print(s)
i am cai,age 18
>>> s = 'i am %(n1)s,age %(n2)d' %{'n1':'cai','n2':18}
>>> print(s)
i am cai,age 18
>>> s = 'i am %(n1)+10s,age %(n2)d' %{'n1':'cai','n2':18}
>>> print(s)
i am cai,age 18
>>> s = 'i am %(n1)+10s,age %(n2)10d' %{'n1':'cai','n2':18}
>>> print(s)
i am cai,age 18
>>> s = "i am %.3f abcd" %1.2
>>> print(s)
i am 1.200 abcd
2、format方式、
i1 = "i am {},age {} ,{}".format('cairui',18,'kk') print(i1) i am cairui,age 18 ,kk i1 = "i am {0},age {1} ,{0}".format('cairui',18) print(i1) i am cairui,age 18 ,cairui i1 = "i am {name},age {age} ,{name}".format(name='cairui',age=18) print(i1) i am cairui,age 18 ,cairui i1 = "i am {:s},age {:d} ,{:f}".format('cairui',18,6.1) print(i1) i am cairui,age 18 ,6.100000
以上是Python字串格式化的方法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python腳本在Unix系統上無法運行的原因包括:1)權限不足,使用chmod xyour_script.py賦予執行權限;2)Shebang行錯誤或缺失,應使用#!/usr/bin/envpython;3)環境變量設置不當,可打印os.environ調試;4)使用錯誤的Python版本,可在Shebang行或命令行指定版本;5)依賴問題,使用虛擬環境隔離依賴;6)語法錯誤,使用python-mpy_compileyour_script.py檢測。

使用Python數組比列表更適合處理大量數值數據。 1)數組更節省內存,2)數組對數值運算更快,3)數組強制類型一致性,4)數組與C語言數組兼容,但在靈活性和便捷性上不如列表。

列表列表更好的forflexibility andmixDatatatypes,何時出色的Sumerical Computitation sand larged數據集。 1)不可使用的列表xbilese xibility xibility xibility xibility xibility xibility xibility xibility xibility xibility xibles and comply offrequent elementChanges.2)

numpymanagesmemoryforlargearraysefefticefticefipedlyuseviews,副本和內存模擬文件.1)viewsAllowSinglicingWithOutCopying,直接modifytheoriginalArray.2)copiesCanbecopy canbecreatedwitheDedwithTheceDwithThecevithThece()methodervingdata.3)metservingdata.3)memore memore-mappingfileShessandAstaStaStstbassbassbassbassbassbassbassbassbassbassbb

Listsinpythondonotrequireimportingamodule,helilearraysfomthearraymoduledoneedanimport.1)列表列表,列表,多功能和canholdMixedDatatatepes.2)arraysaremoremoremoremoremoremoremoremoremoremoremoremoremoremoremoremoremeremeremeremericdatabuteffeftlessdatabutlessdatabutlessfiblesible suriplyElsilesteletselementEltecteSemeTemeSemeSemeSemeTypysemeTypysemeTysemeTypysemeTypepe。

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

記事本++7.3.1
好用且免費的程式碼編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能