Python--format()学习记录
生活随笔
收集整理的這篇文章主要介紹了
Python--format()学习记录
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
.format():格式化輸出字符串
示例:
age = 25 name = 'Caroline' print('{0} is {1} years old. '.format(name, age)) #輸出參數(shù) print('{0} is a girl. '.format(name)) print('{0:.3} is a decimal. '.format(1/3)) #小數(shù)點(diǎn)后三位 print('{0:_^11} is a 11 length. '.format(name)) #使用_補(bǔ)齊空位 print('{first} is as {second}. '.format(first=name, second='Wendy')) #別名替換 print('My name is {0.name}'.format(open('out.txt', 'w'))) #調(diào)用方法輸出:
Caroline is 25 years old. Caroline is a girl. 0.333 is a decimal. _Caroline__ is a 11 length. Caroline is as Wendy. My name is out.txt增補(bǔ)知識(shí)點(diǎn):
填充與對(duì)齊
填充常跟對(duì)齊一起使用
^、<、>分別是居中、左對(duì)齊、右對(duì)齊,后面帶寬度
:號(hào)后面帶填充的字符,只能是一個(gè)字符,不指定的話默認(rèn)是用空格填充
比如
歡迎留言交流!
總結(jié)
以上是生活随笔為你收集整理的Python--format()学习记录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python3--爬取数据之911网站信
- 下一篇: Python--切片学习记录