Python-格式化字符串

来源:互联网 发布:notepad 安装sql插件 编辑:程序博客网 时间:2024/04/30 16:20

格式 描述

%% 百分号标记 #就是输出一个%

%c 字符及其ASCII码

%s 字符串

%d 有符号整数(十进制)

%u 无符号整数(十进制)

%o 无符号整数(八进制)

%x 无符号整数(十六进制)

%X 无符号整数(十六进制大写字符)

%e 浮点数字(科学计数法)

%E 浮点数字(科学计数法,用E代替e)

%f 浮点数字(用小数点符号)

%g 浮点数字(根据值的大小采用%e或%f)

%G 浮点数字(类似于%g)

%p 指针(用十六进制打印值的内存地址)

%n 存储输出字符的数量放进参数列表的下一个变量中


%s 格式化为字符串

[html] view plain copy
  1. >>> format = "Hello, %s. %s enough for ya?"  
  2. >>> values = ('world', 'Hot')  
  3. >>> print format % values  
  4. Hello, world. Hot enough for ya?  
%f 格式化为实数(浮点数)

[html] view plain copy
  1. >>> format = "Pi with three decimals: %.3f"//保留小数点后3个有效数字  
  2. >>> from math import pi//导入pi的值  
  3. >>> print format % pi  
  4. Pi with three decimals: 3.142  


模板字符串:

关键字参数(substitute):

单词替换

[html] view plain copy
  1. >>> from string import Template  
  2. >>> s = Template('$x, gloriout $x!')  
  3. >>> s.substitute(x = 'slurm')  
  4. 'slurm, gloriout slurm!'  
单词字母替换

[html] view plain copy
  1. >>> from string import Template  
  2. >>> s = Template("It's ${x}tastic!")  
  3. >>> s.substitute(x = 'slurm')  
  4. "It's slurmtastic!"  
用$$插入$符号

[html] view plain copy
  1. >>> from string import Template  
  2. >>> s = Template("Make $ selling $x!")  
  3. >>> s.substitute(x = 'slurm')  
  4. 'Make $ selling slurm!'  
字典变量提供值

[html] view plain copy
  1. >>> from string import Template  
  2. >>> s = Template('A $thing must never $action')  
  3. >>> d = {}  
  4. >>> d['thing'] = 'gentleman'  
  5. >>> d['action'] = 'show his socks'  
  6. >>> s.substitute(d)  
  7. 'A gentleman must never show his socks'  

用*作为字段宽度或精度

[html] view plain copy
  1. >>> '%.*s' % (5, 'Guido van Rossum')  
  2. 'Guido'  

转换标志:

-:左对齐

+:在转换值之前加上正负号

“ ”:正数之前保留空格

0:转换值若位数不够用0填充

[html] view plain copy
  1. >>> pi = 3.1415  
  2. >>> '%010.2f' % pi  
  3. '0000003.14'  
  4. >>> '%-10.2f' % pi  
  5. '3.14      '  
  6. >>> '%10.2f' % pi  
  7. '      3.14'  
  8. >>> '% 10.2f' % pi  
  9. '      3.14'  
  10. >>> '%+10.2f' % pi  
  11. '     +3.14'  

字符串格式化范例

[html] view plain copy
  1. width = input('Please enter width: ')  
  2.   
  3. price_width = 10  
  4. item_width = width - price_width  
  5.   
  6. header_format = '%-*s%*s'  
  7. format = '%-*s%*.2f'  
  8.   
  9. print '=' * width  
  10.   
  11. print header_format % (item_width, 'Item', price_width, 'Price')  
  12.   
  13. print '-' * width  
  14.   
  15. print format % (item_width, 'Apples', price_width, 0.4)  
  16. print format % (item_width, 'Pears', price_width, 0.5)  
  17. print format % (item_width, 'Cantaloupes', price_width, 1.92)  
  18. print format % (item_width, 'Dried Apricots (16 oz.)', price_width, 8)  
  19. print format % (item_width, 'Prunes (4 lbs.)', price_width, 12)  

结果显示:

[html] view plain copy
  1. Please enter width: 35  
  2. ===================================  
  3. Item                          Price  
  4. -----------------------------------  
  5. Apples                         0.40  
  6. Pears                          0.50  
  7. Cantaloupes                    1.92  
  8. Dried Apricots (16 oz.)        8.00  
  9. Prunes (4 lbs.)               12.00  

参考:

https://www.douban.com/note/269665597/

http://blog.csdn.net/sheila_1988/article/details/7242720

0 0