将字符按指定格式输出,可以在Python2.7的chm帮助文档中检索:formatting
可找到fromatting.string(%)帮助主题
比如:
>>> print(‘输出数字:%d‘%5) 输出数字:5 >>> print(‘输出浮点数字:%f‘%0.005) 输出浮点数字:0.005000 >>> print(‘输出十六进制数字:%x‘%108) 输出十六进制数字:6c >>> print(‘输出百分比数字:%f%%‘%10.8) 输出百分比数字:10.800000% >>> num=123 >>> ‘dec:%d;oct:%#o;hex:%#X‘%(num,num,num) ‘dec:123;oct:0173;hex:0X7B‘ >>> ‘mm/dd/yy=%02d/%02d/%d‘%(2,16,90) ‘mm/dd/yy=02/16/90‘ >>> print(‘Host:%s\tPort:%d‘%(‘local‘,8080)) Host:local Port:8080 >>> w,i=‘web‘,‘index‘ >>> ‘http://www.baidu.com/%s/%s.html‘%(w,i) ‘http://www.baidu.com/web/index.html‘ >>> for i in range(9): print(‘http://xxx.com/page_%d.htm‘%i) http://xxx.com/page_0.htm http://xxx.com/page_1.htm http://xxx.com/page_2.htm http://xxx.com/page_3.htm http://xxx.com/page_4.htm http://xxx.com/page_5.htm http://xxx.com/page_6.htm http://xxx.com/page_7.htm http://xxx.com/page_8.htm
其它函数
>>> a=‘a‘ >>> b=‘b‘ >>> c=‘c‘ >>> cmp(a,b) -1 >>> cmp(c,b) 1 >>> cmp(a,a) 0 >>> a=‘a‘ >>> b=‘b‘ >>> c=‘c‘ >>> cmp(a,b) -1 >>> cmp(c,b) 1 >>> cmp(a,a) 0 >>> max(‘abcdef‘) ‘f‘ >>> min(‘abce‘) ‘a‘ >>> chr(65) ‘A‘ >>> ord(‘a‘) 97
更多字符串操作内建函数可通过dir(string)查询