重拾Python 八

来源:互联网 发布:2016电影采集网站源码 编辑:程序博客网 时间:2024/04/29 08:19

更多见识字符串格式化,见ex8.py:

#!/usr/bin/python# -*- coding: utf-8 -*-formatstr = "[%r][%r][%r][%r]"print formatstr % (1,2,3,4)print formatstr % ('one', 'two', 'three', 'four')print formatstr % (True, False, True, False)print formatstr % (formatstr, formatstr, formatstr, formatstr)print formatstr % (                "I am a stupid coder.",                "I used to learn python.",                "But I did not cherish it.",                "Now I am aware of it."                )

执行结果:

[1][2][3][4]['one']['two']['three']['four'][True][False][True][False]['[%r][%r][%r][%r]']['[%r][%r][%r][%r]']['[%r][%r][%r][%r]']['[%r][%r][%r][%r]']['I am a stupid coder.']['I used to learn python.']['But I did not cherish it.']['Now I am aware of it.']

格式化字符串中的占位符:

符 号 说 明 %c 格式化字符及其ASCII码 %s 格式化字符串 %d 格式化整数 %u 格式化无符号整型 %o 格式化无符号八进制数 %x 格式化无符号十六进制数 %X 格式化无符号十六进制数(大写) %f 格式化浮点数字,可指定小数点后的精度 %e 用科学计数法格式化浮点数 %E 作用同%e,用科学计数法格式化浮点数 %g 根据值的大小决定使用%f活%e %G 作用同%g,根据值的大小决定使用%f活%e %p 用十六进制数格式化变量的地址
0 0
原创粉丝点击