Python format 格式化 总结 大量 实例
来源:互联网 发布:程序员杂志 2017 pdf 编辑:程序博客网 时间:2024/05/22 12:50
测试format的不同用法
# 格式化{{}} 等于{}print('{{ hello {0} }}'.format('Kevin')) # { hello Kevin }# test 同一个参数指定多个位置print('\n 同一个参数多个位置调用')print('Hi, I am {0}, my {1} is {0}'.format('jimmy', 'name')) # Hi, I am jimmy, my name is jimmy# 对齐与填充print('\n 对齐与填充')import mathprint('{:o<10}'.format(12345)) # 12345ooooo 左对齐 填充o 宽度10print('{:x>20.15f}'.format(math.pi)) # xxx3.141592653589793 右对齐 填充x 保留小数15 宽度20print('{:x> 20.15f}'.format(math.pi)) # xx 3.141592653589793 右对齐 填充x 保留小数15 宽度20print('{:x>+20.15f}'.format(math.pi)) # xx+3.141592653589793 右对齐 填充x 保留小数15 宽度20print('{:x>+20.15f}'.format(-3.141592653589793111)) # xx-3.141592653589793 右对齐 填充x 保留小数15 宽度20print('{:+^10d}'.format(123)) # +++123++++ 中间对齐 填充+ 宽度10print('{:,}'.format(1234567890)) # 1,234,567,890 以逗号分隔的数字格式print('{:.2%}'.format(0.1)) # 10.00% 百分比格式 保留小数2# 通过字典设置参数print('\n 通过字典调用')dict2 = {'name': 'jimmy', 'gender': 'male'}print('my name is: {name}, gender is {gender}'.format(**dict2)) # 注意需要两个**在字典之前 my name is: jimmy, gender is maleprint('name:{d[name]}, gender:{d[gender]}'.format(d=dict2)) # 赋值给d name:jimmy, gender:maleprint('\n 序列化字典的输出')dict1 = {'jimmy': 100, 'tom': 88, 'penny': 99}for en, (k, v) in enumerate(dict1.items(),1): # 把每一个 item 绑定序号, enumerate([迭代器], [开始的号码]) print('NO:{{{}}} ---- name: {:<6} score: {:>3}'.format(en, k, v))# NO:{1} ---- name: jimmy score: 100# NO:{2} ---- name: tom score: 88# NO:{3} ---- name: penny score: 99# 调用类的属性和方法print('\n 调用类的属性方法')class Teacher(): job = 'teacher' def speak(self): return 'good good study!!!'print('as a {t.job}, I have to talk you that {t.speak}'.format(t=Teacher())) # 无法在{}里面调用 类的方法# as a teacher, I have to talk you that <bound method Teacher.speak of <__main__.Teacher object at 0x000002057CC8A240>>print('as a {}, I have to talk you that {}'.format(Teacher().job, Teacher().speak())) # 无法调用 类的方法# as a teacher, I have to talk you that good good study!!!# 魔法参数print('\n 魔法参数')args = ['jimmy', 'penny']kwargs = {'name1': 'jack', 'name2': 'king'}print("this is {1} calling, I want to speak with {name2}, tell him {0} married with {name1}".format(*args, **kwargs))# this is penny calling, I want to speak with king, tell him jimmy married with jack# 调用函数print('\n 调用函数')def speak(): return 'do you think am I'print('{} very good?'.format(speak()))# do you think am I very good?# 设置格式print('\n 设置格式')text1 = 'I feel good'text1_formated = '{:-^30}'.format(text1)print(text1_formated) # ---------I feel good----------text1_formated2 = '{:=^50}'.format(text1_formated)print(text1_formated2) # ==========---------I feel good----------==========# 浓缩一下def text_formater(text): text_formated = '{:=^50} '.format(' {:-^30} '.format(' ' + str(text) + ' ')) print(text_formated)text_formater('Tile 123') # ========= ---------- Tile 123 ---------- =========
阅读全文
0 0
- Python format 格式化 总结 大量 实例
- Python中print/format字符串格式化实例
- %格式化和format格式化--python
- python format格式化字符串
- Python 格式化format
- python.format() 格式化字符串
- Python格式化字符串format
- python-格式化输出-format
- Python format 格式化函数
- python:格式化字符串,format
- Python string格式化format
- String.format() 字符串格式化 实例
- String.format() 字符串格式化 实例
- python 格式化输出 format()
- python之字符串格式化(format)
- Python中用format()格式化字符串
- day14 Python format字符串格式化
- python-format函数格式化字符串
- Node学习笔记(一)
- JS中关于clientWidth offsetWidth scrollWidth 等的含义
- 被穷养长大是一种怎样的人生体验?看完好心酸,这拼爹妈的年代!
- 谷歌将加倍努力打击极端主义
- PHP 7 vs HHVM 比较
- Python format 格式化 总结 大量 实例
- Linux之工具------sed
- usb基础知识资料
- myeclipse exclude from validation
- 数据挖掘经典算法--CART算法分类和回归树
- 深入理解线程池
- Grunt wiredep:app no such file or directory bower.json
- Redis + Jedis + Spring (三)—— 列表操作
- iframe与父窗口通信,并操作父窗口