zoukankan      html  css  js  c++  java
  • 【Python】Python 打印和输出更多用法。

    Python 打印和输出

    简述

    在编程实践中,print 的使用频率非常高,特别是程序运行到某个时刻,要检测产生的结果时,必须用 print 来打印输出。

    关于 print 函数,前面很多地方已经提及过,可用于写入标准输出。现在,是时候该深入了。

    注意:这里强调的是“print 函数”,而不是“print 语句”。

    深入 print

    在 Python 2.x 中,print 是一个语句,但是在 Python 3.x 中,它是一个函数。如果 2.x 和 3.x 都使用过,你就会发现差异有多么大。

    进入 3.x 的交互式 shell,尝试使用“print 语句”:

    [wang@localhost ~]$ python
    Python 3.5.2 (default, Mar 29 2017, 11:05:07) 
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    >>> print 'Python'
    ...
    SyntaxError: Missing parentheses in call to 'print'
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    对于大多数人来说,这个错误信息再熟悉不过了。正如上面所提到的那样,print 是 3.x 中的一个函数,与其他函数一样,参数应该被圆括号括起来:

    >>> print('Python')
    Python
    • 1
    • 2

    print 函数

    要了解 print 函数的用途,可以使用 help() 来寻求帮助:

    >>> help(print)
    ...
    Help on built-in function print in module builtins:
    
    print(...)
        print(value, ..., sep=' ', end='
    ', file=sys.stdout, flush=False)
    
        Prints the values to a stream, or to sys.stdout by default.
        Optional keyword arguments:
        file:  a file-like object (stream); defaults to the current sys.stdout.
        sep:   string inserted between values, default a space.
        end:   string appended after the last value, default a newline.
        flush: whether to forcibly flush the stream.
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    将对象输出到文本流文件,由 sep 分开,然后以 end 结束。如果 sep、end、file 和 flush 出现,则必须以关键字参数的形式指定。

    不使用关键字参数

    print 函数可以打印任意数量的值(value1, value2, …),这些值由逗号分隔。

    >>> age = 18
    >>> 
    >>> print('age', age)
    age 18
    • 1
    • 2
    • 3
    • 4

    很容易发现,两个值之间有一个分隔符 - 空格(默认值),这取决于 sep。

    分隔符

    如果要重新定义分隔符,可以通过 sep 来指定。

    >>> print('age', age, sep='')  # 去掉空格
    age18
    >>> 
    >>> print('www', 'python', 'org', sep='.')  # 以 . 分割
    www.python.org
    • 1
    • 2
    • 3
    • 4
    • 5

    结束符

    在 print 中,字符串后面会跟一个  (换行),前面的示例体现的不是很明显,换一个方式就显示出来了。

    >>> for letter in 'Python':
    ...     print(letter)
    ... 
    P
    y
    t
    h
    o
    n
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    每打印一个就换行,再打印下一个,这就是   所起的作用。

    要改变这种行为,可以给 end 分配一个任意字符串:

    >>> for letter in 'Python':
    ...     print(letter, end='-')
    ... 
    P-y-t-h-o-n->>> 
    • 1
    • 2
    • 3
    • 4

    输出重定向

    默认情况下,print 的输出被发送到标准输出流(sys.stdout)。通过重新定义 file,可以将输出发送到不同的流(例如:文件或 sys.stderr)中。

    >>> f = open('data.txt', 'w')
    >>> print('I am a Pythonista', file=f)
    >>> f.close()
    • 1
    • 2
    • 3

    可以看到,在交互式 shell 中,没有得到任何输出,输出被发送到文件 data.txt 中:

    [wang@localhost ~]$ cat data.txt 
    I am a Pythonista
    • 1
    • 2

    也可以通过这种方式将输出重定向到标准错误(sys.stderr)通道:

    >>> import sys
    >>> 
    >>> print('age: 18', file=sys.stderr)
    age: 18
    • 1
    • 2
    • 3
    • 4

    输出是否缓冲通常由文件决定,但是如果 flush 是 true,则流将被强制刷新。

    Python3 格式化输出 %s & %d 等

    1.打印字符串

    print("My name is %s" %("Alfred.Xue"))
    #输出效果:
    My name is Alfred.Xue

    2.打印整数

    print("I am %d years old." %(25))
    #输出效果:
    I am 25 years old.

    3.打印浮点数

    print ("His height is %f m"%(1.70))
    #输出效果:
    His height is 1.700000 m

    4.打印浮点数(指定保留两位小数)

    print ("His height is %.2f m"%(1.70))
    #输出效果: His height is 1.70 m

    5.指定占位符宽度

    print ("Name:%10s Age:%8d Height:%8.2f"%("Alfred",25,1.70))
    #输出效果: Name: Alfred Age: 25 Height: 1.70

    6.指定占位符宽度(左对齐)

    print ("Name:%-10s Age:%-8d Height:%-8.2f"%("Alfred",25,1.70))
    #输出效果:
    Name:Alfred     Age:25       Height:1.70

    7.指定占位符(只能用0当占位符?)

    print ("Name:%-10s Age:%08d Height:%08.2f"%("Alfred",25,1.70))
    #输出效果: Name:Alfred Age:00000025 Height:00001.70

    8.科学计数法

    format(0.0026,'.2e')
    #输出效果: '2.60e-03'

    字符串格式化代码:

    格式描述
    %% 百分号标记
    %c 字符及其ASCII码
    %s 字符串
    %d 有符号整数(十进制)
    %u 无符号整数(十进制)
    %o 无符号整数(八进制)
    %x 无符号整数(十六进制)
    %X 无符号整数(十六进制大写字符)
    %e 浮点数字(科学计数法)
    %E 浮点数字(科学计数法,用E代替e)
    %f 浮点数字(用小数点符号)
    %g 浮点数字(根据值的大小采用%e或%f)
    %G 浮点数字(类似于%g)
    %p 指针(用十六进制打印值的内存地址)
    %n 存储输出字符的数量放进参数列表的下一个变量中

  • 相关阅读:
    数字图像处理的Matlab实现(2)—MATLAB基础
    数字图像处理的Matlab实现(1)—绪论
    统计分析与R软件-chapter2-6
    统计分析与R软件-chapter2-5
    统计分析与R软件-chapter2-3
    题目1143:Primary Arithmetic
    剑指OFFER之翻转单词顺序
    剑指OFFER之把数组排成最小的数
    剑指OFFER之丑数
    最大的两个数
  • 原文地址:https://www.cnblogs.com/zhuzhubaoya/p/9272137.html
Copyright © 2011-2022 走看看