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