python--格式化输出

1-字符串格式化输出方法一: %    

 1-print('名字是 %s,年龄是%s' % (name ,age))
    2- %s ---
字符串-----相当于执行了str()
    3- (name ,age) 
只能是元组,不能是列表
    4-
多个数据的打印,一定是元组

%后字符含义:

  • %s:str,字符类型,用str()方法处理对象
  • %d(i):decimal,十进制数
  • %x: hex, 十六进制数=》%0x5a
  • %f: float,浮点数
  • %r:类似于%s类型,用rper()方法处理对象,此外用%r打印时能够重现它所代表的对象 (拓展:Difference between __str__ and __repr__

%s

  • %10s——右对齐,占位符10位
  • %-10s——左对齐,占位符10位
  • %.2s——截取2位字符串
  • %10.2s——10位占位符,截取两位字符串复制代码
 1 >>> print('%s' % 'hello world')  # 字符串输出
 2 hello world
 3 >>> print('%20s' % 'hello world')  # 右对齐,取20位,不够则补位
 4          hello world
 5 >>> print('%-20s' % 'hello world')  # 左对齐,取20位,不够则补位
 6 hello world         
 7 >>> print('%.2s' % 'hello world')  # 取2位
 8 he
 9 >>> print('%10.2s' % 'hello world')  # 右对齐,取2位
10         he
11 >>> print('%-10.2s' % 'hello world')  # 左对齐,取2位
12 he    

指定长度:正数---右对齐,左补齐--补空格; 负数---左对齐,右补齐--补空格

  •  %5d:右对齐,不足左边补空格
  •  %-5d:- 代表左对齐,不足右边默认补空格
  •  %05d:右对齐,不足左边补0      

 浮点数:

  • %f: 默认是输出6位有效数据, 会进行四舍五入
  • %.8f:指定小数点位数的输出,保留小数点后8位
  • %4.8f:4代表整个浮点数的长度,包括小数,(包括 . ),只有当字符串的长度大于4位才起作用
  • %08.3f :补0

   

print('%f' % 1.11)  # 默认保留6位小数
结果:1.110000
print('%.1f' % 1.11)  # 取1位小数
结果:1.1

(1)字符串格式代码

(2)常用转义字符

2-字符串格式化输出方法二: format()---固定的 {}    

 1- 顺序填坑:
        1-
可以有元素多,不能有元素少!
        print('
名字是 {},年龄是 {}'.format(name ,age))
       

print('{} {}'.format('hello','world'))  # 不带字段
结果:hello world

    2- 下标填坑:
        1-
不能下标越界 IndexError: tuple index out of range
        print('
名字是 {1},年龄是 {0}'.format(name ,age))

print('{0} {1}'.format('hello','world'))  # 带数字编号
结果:hello world
print('{0} {1} {0}'.format('hello','world'))  # 打乱顺序
结果:hello world hello
print('{1} {1} {0}'.format('hello','world'))
结果:world world hello      

    3- 变量方法
        1- print('
名字是 {name},年龄是 {age}'.format(name='tom' ,age = 18))
       

print('{a} {tom} {a}'.format(tom='hello',a='world'))  # 带关键字
结果:world hello world

    4-指定长度输出:
        1- {:
长度}
            1-
数值型:右对齐,左补齐
            2-
字符串:左对齐,右补齐
        2- > 
右对齐
        3- < 
左对齐
        4- ^
中间对齐  ---异或
        5-
数值补0 ,一般是右对齐 , 左补0 ,不改变值
        6-
字符串本身带花括号  {{}}

info = '我叫:{:*>6},年龄是:{:0>6}'.format(name,age)       

格式转换

  'b' - 二进制。将数字以2为基数进行输出。
  'c' - 字符。在打印之前将整数转换成对应的Unicode字符串。
  'd' - 十进制整数。将数字以10为基数进行输出。
  'o' - 八进制。将数字以8为基数进行输出。
  'x' - 十六进制。将数字以16为基数进行输出,9以上的位数用小写字母。
  'e' - 幂符号。用科学计数法打印数字。用'e'表示幂。
  'g' - 一般格式。将数值以fixed-point格式输出。当数值特别大的时候,用幂形式打印。
  'n' - 数字。当值为整数时和'd'相同,值为浮点数时和'g'相同。不同的是它会根据区域设置插入数字分隔符。
  '%' - 百分数。将数值乘以100然后以fixed-point('f')格式打印,值后面会有一个百分号。
复制代码
复制代码
 1 >>> print('{0:b}'.format(3))
 2 11
 3 >>> print('{:c}'.format(20))
 4 
 5 >>> print('{:d}'.format(20))
 6 20
 7 >>> print('{:o}'.format(20))
 8 24
 9 >>> print('{:x}'.format(20))
10 14
11 >>> print('{:e}'.format(20))
12 2.000000e+01
13 >>> print('{:g}'.format(20.1))
14 20.1
15 >>> print('{:f}'.format(20))
16 20.000000
17 >>> print('{:n}'.format(20))
18 20
19 >>> print('{:%}'.format(20))
20 2000.000000%
21 >>> 
复制代码
复制代码

进制转换

>>> # format also supports binary numbers
>>> "int: {0:d};  hex: {0:x};  oct: {0:o};  bin: {0:b}".format(42)
'int: 42;  hex: 2a;  oct: 52;  bin: 101010'
>>> # with 0x, 0o, or 0b as prefix:
>>> "int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}".format(42)  # 在前面加“#”,则带进制前缀
'int: 42;  hex: 0x2a;  oct: 0o52;  bin: 0b101010'

百分数%

>>> points = 19
>>> total = 22
>>> 'Correct answers: {:.2%}'.format(points/total)
'Correct answers: 86.36%'

时间

>>> import datetime
>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
>>> '{:%Y-%m-%d %H:%M:%S}'.format(d)
'2010-07-04 12:15:58'

逗号 ‘,’ 分割金钱

>>> '{:,}'.format(1234567890)
'1,234,567,890'

3- python 3.6 以后   f''       

# a.format(b)
>>> "{0} {1}".format("hello","world")
'hello world'


# f"xxxx"
# 可在字符串前加f以达到格式化的目的,在{}里加入对象,此为format的另一种形式:

>>> a = "hello"
>>> b = "world"
>>> f"{a} {b}"
'hello world'



name = 'jack'
age = 18
sex = 'man'
job = "IT"
salary = 9999.99

print(f'my name is {name.capitalize()}.')
print(f'I am {age:*^10} years old.')
print(f'I am a {sex}')
print(f'My salary is {salary:10.3f}')

# 结果
my name is Jack.
I am ****18**** years old.
I am a man
My salary is   9999.990



原文地址:https://www.cnblogs.com/guang2508/p/12957267.html