一、格式化字元功能介紹
應用場景:一般在print的時候提供佔位符;python中提供兩種格式化字串方式:第一種是古老的利用百分號的方式,第二種是增強的格式化字串、format 函數。
二、古老的百分號方式
1、常見用法
## 常見佔位符:%s -------------字串佔位符 %d -------------數位佔位符# 〠 〠〠 ---- 〠 ---- -浮點型數字佔位符
例如:輸入使用者的訊息,並列印出來name= raw_input('input your name: ') age= raw_input ('input your age: ') job = raw_input ('input your job: ') msg=''' #定义变量msg,字符串中提供占位符 Information of user %s: ------------------------------- Name: %s Age: %s #特别注意这里,像age这种通过raw_input方式定义的变量,python默认都为字符串,因此这里不用%d,而是用%s #如果非要用%d的话,那么可以在定义age的时候用函数转换 age= int(raw_input ('input your age: ')) Job: %s --------------End-------------- '''%(name,name,age,job) #把3个用户定义的变量依次赋给msg字符串中的各个占位符,实现替换,注意4个要按顺序一一对应 print msg 结果: Information of user Mr.Mangood: ------------------------------- Name: Mr.Mangood Age: 27 Job: Engineer --------------End--------------2、更多進階用法佔位元符結構:
%[(name)][flags][width].[precision]typecode
其中:
>>> info = '''联系人%(name)s的年龄是%(age)d'''%{'name':'Mr.Mangood','age':23} # 这里可以用字典{'name':'Mr.Mangood','age':23}来对占位符传值 >>> print info 联系人Mr.Mangood的年龄是23
[flags] >>> info = '''联系人%(name)20s的年龄是%(age)d'''%{'name':'Mr.Mangood','age':23} # 这里的20就是宽度[width],不写[flags]默认右对齐
>>> print info
联系人 Mr.Mangood的年龄是23
>>> info = '''联系人%(name)+20s的年龄是%(age)d'''%{'name':'Mr.Mangood','age':25} # 这里的+20就是右对齐
>>> print info
联系人 Mr.Mangood的年龄是25
>>> info = '''联系人%(name)-20s的年龄是%(age)d'''%{'name':'Mr.Mangood','age':23} # 这里的-20就是代表左对齐
>>> print info
联系人Mr.Mangood 的年龄是23
右對齊
- - ## 0右對齊
#[width] 可選,定義佔位符寬度
>>> info = '''学生%(name)s的年龄是%(age)d平均成绩为%(avgscore).2f'''%{'name':'Mr.Mangood','age':25,'avgscore':85.2479} #这里的.2就代表%f这个浮点型占位符保留两位小数 >>> print info 学生Mr.Mangood的年龄是25平均成绩为85.25
.precision 後數位小位數##
>>> s = '''测试ASCII码转换%c测试八进制转换%o测试16进制转换%x'''%(65,9,15) >>> print s 测试ASCII码转换A测试八进制转换11测试16进制转换f
typecode 必選
c %#將傳回傳#」的數字
o 將整數轉換成八進位表示rr x
## rr x## r e 將整數、浮點數轉換成科學計數法(科學計數法中的e為小寫的e)
E 科學、將整數、浮點數、浮點科學數(整數科學(法數法(計算科學)(整數法數法(整數科學法(2)法數:計算科學數法(2227法科學 和計算科學)(整數科學]中的E為大寫的E)
>>> test = '''测试科学技术法%e测试科学技术法%E'''%(98888888,98888888) >>> print test 测试科学技术法9.888889e+07测试科学技术法9.888889E+07g 自動辨識數字大小是否需要轉換成科學計數法(如果超過6位數,就用科學計數法來表示),科學計數法是否需要轉換為科學計數法(如果超過6位數,就用科學計數法來表示),科學計數法是否需要轉換計數法中的e為小寫
G 自動辨識數字大小是否需要轉換成科學計數法(若超過6位數中的G為大寫
>>> test = '''测试科学技术法%e测试科学技术法%g'''%(123456,123456) >>> print test 测试科学技术法1.234560e+05测试科学技术法123456% 當字串中出現了格式化標誌時,則需要用%%表示一個%
>>> zhenli = '''%s是%d%%纯爷们'''%('Mr.Mangood',100) >>> print zhenli Mr.Mangood是100%纯爷们
以上是python格式化字串的使用方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。

toAccesselementsInapythonlist,useIndIndexing,負索引,切片,口頭化。 1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

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

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

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

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具