亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python中.format()方法使用詳解

 更新時(shí)間:2022年07月18日 09:23:41   作者:IT之一小佬  
這篇文章主要介紹了python中.format()方法使用詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下

前言

format語法格式:

  • str.format()
  • str是指字符串實(shí)例對(duì)象,常用格式為‘ ’.format()
    def format(self, *args, **kwargs): # known special case of str.format
        """
        S.format(*args, **kwargs) -> str

        Return a formatted version of S, using substitutions from args and kwargs.
        The substitutions are identified by braces ('{' and '}').
        """
        pass

format參數(shù)格式:

'{[index][ : [fill] align][sign][#][width][.precision][type]} {……}{……} '.format()
注意,格式中的[ ]內(nèi)的參數(shù)都是可選參數(shù),可以使用也可以不使用

  • index:指定冒號(hào)**:**后面出現(xiàn)的參數(shù)在‘format()’中的索引值,如果沒有,則以format()中的默認(rèn)順序自動(dòng)分配
  • fill:指定空白處的填充符。align:指定數(shù)字的對(duì)齊方式:align含義<right-aligned 左對(duì)齊(對(duì)于大部分對(duì)象時(shí)為默認(rèn))>right-aligned 右對(duì)齊 (對(duì)于數(shù)字時(shí)為默認(rèn))=數(shù)據(jù)右對(duì)齊,同時(shí)將符號(hào)放置在填充內(nèi)容的最左側(cè),該選項(xiàng)只對(duì)數(shù)字類型有效^數(shù)據(jù)居中,此選項(xiàng)需和 width 參數(shù)一起使用
  • sign:指定有無符號(hào)數(shù),此參數(shù)的值以及對(duì)應(yīng)的含義如表所示sign 參數(shù)含義+正數(shù)前面添加 ‘ + ’ ,負(fù)數(shù)前面加 ‘ - ’-正數(shù)前面不添加 ‘ + ’ ,負(fù)數(shù)前面加 ‘ - ’space正數(shù)前面添加 ‘ 空格 ’ ,負(fù)數(shù)前面加 ‘ - ’#對(duì)于二進(jìn)制數(shù)、八進(jìn)制數(shù)和十六進(jìn)制數(shù),使用此參數(shù),各進(jìn)制數(shù)前會(huì)分別顯示 0b、0o、0x前綴;反之則不顯示前綴width:指定輸出數(shù)據(jù)時(shí)所占的寬度. precision:如果后面存在type參數(shù),則指的是保留小數(shù)的位數(shù),如果type參數(shù)不存在,則是指有效數(shù)字的位數(shù)type:指定輸出數(shù)據(jù)的具體類型

一、簡單使用方法

1.無參數(shù)

foramt會(huì)把參數(shù)按位置順序來填充到字符串中,第一個(gè)參數(shù)是0,然后1 ……也可以不輸入數(shù)字,則會(huì)按照順序自動(dòng)分配,而且一個(gè)參數(shù)可以多次插入

示例代碼:

name = '張三'
age = 25
sex = '男'
 
print('{}、{}、{}'.format(name, age, sex))  #  占位符不指定順序
print('{0}、{1}、{2}'.format(name, age, sex)) #  占位符制定順序
print('{0}、{2}、{1}'.format(name, age, sex)) #  換一下順序試試
print('{0}、{2}、{1}、{0}、{2}、{1}'.format(name, age, sex))

運(yùn)行結(jié)果:

2. key value

示例代碼:

name1 = '張三'
age1 = 25
sex1 = '男'
 
print('name:{name}、age={age}、sex:{sex}'.format(name=name1, age=age1, sex=sex1))
print('name:{name}、sex:{sex}、age={age}'.format(name=name1, age=age1, sex=sex1))

運(yùn)行結(jié)果:

3. 列表

示例代碼:

lst1 = ['張三', '男', 25]
lst2 = ['李四', '男', 28]
 
print('name:{Lst[0]},sex:{Lst[1]},age:{Lst[2]}'.format(Lst=lst1))
print('name:{0[0]},sex:{0[1]},age:{0[2]}'.format(lst1))
print('name:{0[0]},sex:{0[1]},age:{0[2]}'.format(lst2))
print('name:{0[0]},sex:{0[1]},age:{0[2]}'.format(lst1, lst2))
print('name:{1[0]},sex:{1[1]},age:{1[2]}'.format(lst1, lst2))
print('name:{0[0]},sex:{0[1]},age:{0[2]},name:{1[0]},sex:{1[1]},age:{1[2]}'.format(lst1, lst2))

運(yùn)行結(jié)果:

4. 字典

示例代碼:

dic1 = {'name': '張三', 'sex': '男', 'age': 25}
dic2 = {'name': '李四', 'sex': '男', 'age': 28}
 
print('name:{Dic[name]},sex:{Dic[sex]},age:{Dic[age]}'.format(Dic=dic1))
print('name:{name},sex:{sex},age:{age}'.format(**dic2))

運(yùn)行結(jié)果:

5. 類

示例代碼:

class Info(object):
    name = '張三'
    sex = '男'
    age = 25
print('name:{info.name},sex:{info.sex},age:{info.age}'.format(info=Info))

運(yùn)行結(jié)果:

6. 魔法參數(shù)

*args表示任何多個(gè)無名參數(shù),它是一個(gè)tuple or list;**kwargs表示關(guān)鍵字參數(shù),它是一個(gè) dict。

 示例代碼:

lst = [',', '.']
dic = {'name': '張三', 'sex': '男', 'age': 25}
print('name:{name}{0}sex:{sex}{0}age:{age}{1}'.format(*lst, **dic))

運(yùn)行結(jié)果:

二、參數(shù)使用方法

示例代碼1:

#  python :^:代表居中顯示,數(shù)字567,位數(shù)width=10,fill=*(填充符為*)
print('{:*^10}'.format(567))

運(yùn)行結(jié)果:

 示例代碼2:

# python :0是填充符,2是width,表示位數(shù)為2
print('{:02}:{:02}:{:02}'.format(13, 4, 57))
print('{:05}:{:05}:{:05}'.format(13, 4, 57))

運(yùn)行結(jié)果:

到此這篇關(guān)于python中.format()方法使用詳解的文章就介紹到這了,更多相關(guān)python .format()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論