Python 学习入门(8)—— 格式化输出
2013-11-19 22:33
531 查看
python中也有类似于c中的printf()的格式输出标记,在python格式化使用的是%运算符,示例如下:
Python格式化字符串的替代符以及含义
Python的转义字符及其含义
如果在字符串中输出"\",需使用"\\"
#!/usr/bin/env python # it-homer in 2013 import sys reload(sys) sys.setdefaultencoding("utf-8") # %s 格式化为字符串 from math import pi def test_format(): str = "hello %s, http://blog.%s.net" val = ("it-homer", "ithomer") print str % val # hello it-homer, http://blog.ithomer.net print("%s %s %s" % (1, 2.3, ['one', 'two', 'three'])) # 1 2.3 ['one', 'two', 'three'] print("PI: %.10f" % pi) # PI: 3.1415926536 # -:左对齐; +:在转换值之前加上正负号; # “ ”:正数之前保留空格; 0:转换值若位数不够用0填充 print("PI: %010.2f" % pi) # PI: 0000003.14 print("PI: %010.6f" % pi) # PI: 003.141593 print("PI: %-10.2f" % pi) # PI: 3.14 print("PI: %-10.6f" % pi) # PI: 3.141593 print("PI: %10.2f" % pi) # PI: 3.14 print("PI: %10.6f" % pi) # PI: 3.141593 print("PI: %010.2f" % pi) # PI: 0000003.14 print("PI: % 10.2f" % pi) # PI: 3.14 print("PI: %+10.2f" % pi) # PI: +3.14 print("PI: %-+10.2f" % pi) # PI: +3.14 print("PI: %+-10.2f" % pi) # PI: +3.14 # 模板字符串 from string import Template def test_format2(): s = Template('$x, hello $x') f = s.substitute(x = 'ithomer') print(f) # ithomer, hello ithomer # 单词字母替换 s = Template("It's ${x}homer blog") f = s.substitute(x='it') print(f) # It's ithomer blog # 用$$插入一个$符号 s = Template("It's $$ blog $x") f = s.substitute(x='ithomer') print(f) # It's $ blog ithomer s = Template('A $name go to $blog') d = {} d['name'] = 'sunboy' d['blog'] = 'blog.ithomer.net' f = s.substitute(d) print(f) # A sunboy go to blog.ithomer.net def test_format3(): num = 20 # 十进制 print("%d" % num) # 20 print("%u" % num) # 20 # 八进制 print("%o" % num) # 24 # 十六进制 print("%x" % num) # 14 print("%X" % num) # 14 # 浮点数 print("%f" % num) # 20.000000 # 科学计数法 print("%e" % num) # 2.000000e+01 print("%E" % num) # 2.000000E+01 print("%g" % num) # 20 def test_format4(): print("'ithomer'") # 'ithomer' print("\'ithomer") # 'ithomer print("\"ithomer\"") # "ithomer" print("\\ithomer\\") # \ithomer\ print("/ithomer/") # /ithomer/ print("aa\nbb") # aa next line bb print("aa\bbb") # abb print("aa\rbb") # bb print("aa\tbb") # aa bb print("aa\000bb") # aabb if __name__ == "__main__": test_format() test_format2() test_format3() test_format4()
Python格式化字符串的替代符以及含义
符 号 | 说 明 |
%c | 格式化字符及其ASCII码 |
%s | 格式化字符串 |
%d | 格式化整数 |
%u | 格式化无符号整型 |
%o | 格式化无符号八进制数 |
%x | 格式化无符号十六进制数 |
%X | 格式化无符号十六进制数(大写) |
%f | 格式化浮点数字,可指定小数点后的精度 |
%e | 用科学计数法格式化浮点数 |
%E | 作用同%e,用科学计数法格式化浮点数 |
%g | 根据值的大小决定使用%f活%e |
%G | 作用同%g,根据值的大小决定使用%f活%e |
%p | 用十六进制数格式化变量的地址 |
符 号 | 说 明 |
\' | 单引号 |
\" | 双引号 |
\a | 发出系统响铃声 |
\b | 退格符 |
\n | 换行符 |
\t | 横向制表符 |
\v | 纵向制表符 |
\r | 回车符 |
\f | 换页符 |
\o | 八进制数代表的字符 |
\x | 十六进制数代表的字符 |
\000 | 终止符,\000后的字符串全部忽略 |
相关文章推荐
- Python学习入门基础教程(learning Python)--1.2.3 Python格式化输出百分比
- Python学习入门基础教程(learning Python)--1.2.4 Python格式化输出科学计数 .
- Python 学习入门(8)—— 格式化输出
- Python学习入门基础教程(learning Python)--1.2.2 Python格式化输出基础 .
- Python入门学习之---------Python数字表达式和输出
- python格式化输出学习
- 【零基础入门学习Python笔记015】字符串:格式化
- Python学习-格式化输出(format)
- 编程语言的学习--------python3 列表与原则的嵌套以及格式化输出
- Python入门(二)——IDE选择PyCharm,输入和输出,基础规范,数据类型和变量,常量,字符串和编码,格式化
- python学习习题总结(8)——简单选择排序,列表解析式使用(格式化输出杨辉三角,九九乘法表)
- Python入门教程2. 字符串基本操作【运算、格式化输出、常用函数】[原创]_python_脚本之家
- Python入门(二)——IDE选择PyCharm,输入和输出,基础规范,数据类型和变量,常量,字符串和编码,格式化
- Python3学习笔记【基础2--输出,输入,格式化,编码】
- Python学习笔记2-Python神奇的语法和格式化输出
- 1.30继续python学习 运算符 while for 格式化输出
- Python学习笔记2-Python神奇的语法和格式化输出
- Python学习笔记2-Python神奇的语法和格式化输出
- python学习03--格式化输出
- Python学习入门基础教程(learning Python)--1.2.1 Python输出语句print基本使用 .