zoukankan      html  css  js  c++  java
  • Python3内置函数——print

    英文文档:

    print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)

    Print objects to the text stream file, separated by sep and followed by end. sep, end, file and flush, if present, must be given as keyword arguments.

    All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.

    The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(...) instead.

    Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.

    Changed in version 3.3: Added the flush keyword argument.

    函数信息表格

    函数原型

    print(value,...,sep=‘ ’, end=’\n’, file=sys.stdout, flush=False)

    参数解释

    sep

    分隔符,默认为空格;

    end

    输出结束时补充该参数所指定的字符串,默认为换行符;

    file

    定义流输出的文件,默认为标准的系统输出sys.stdout,可以重定义为别的文件;

    flush

    是否立即把内容输出到流文件,不作缓存,默认为False

    返回值

    <class 'NoneType'> 返回相应的输出结果。

    函数说明

    打印相应的内容。

    print函数的格式化输出

    格式化输出:

    1) %字符:标记转换说明符的开始

    2) 转换标志:-表示左对齐;+表示在转换值之前要加上正负号;“”(空白字符)表示正数之前保留空格;0表示转换值若位数不够则用0填充

    3) 最小字段宽度:转换后的字符串至少应该具有该值指定的宽度。如果是*,则宽度会从值元组中读出。

    4) 点‘.’后跟精度值:如果转换的是实数,精度值就表示出现在小数点后的位数。如果转换的是字符串,那么该数字就表示最大字段宽度。如果是*,那么精度将从元组中读出

    5) 字符串格式化转换类型

    含义

    d,i

    带符号的十进制整数

    o

    不带符号的八进制

    u

    不带符号的十进制

    x

    不带符号的十六进制(小写)

    X

    不带符号的十六进制(大写)

    e

    科学计数法表示的浮点数(小写)

    E

    科学计数法表示的浮点数(大写)

    f,F

    十进制浮点数

    g

    如果指数大于-4或者小于精度值则和e相同,其他情况和f相同

    G

    如果指数大于-4或者小于精度值则和E相同,其他情况和F相同

    C

    单字符(接受整数或者单字符字符串)

    r

    字符串(使用repr转换任意python对象)

    s

    字符串(使用str转换任意python对象)

    范例1:基本的打印输出(Python 3.6.2 shell 环境)

    1 >>> print(1,'2',[3.40,'5'],(6,[7,8],'9'))            #参数缺省
    2 1 2 [3.4, '5'] (6, [7, 8], '9')
    3 >>> print(1, '2', 3.00, sep = '|', end = '\nline2')  #使用'|'作为分隔符,'\nline2'为结束符
    4 1|2|3.0                                             
    5 line2
    6 >>> print(1, '2', 3.00, sep = '', end = '')          #这里需要注意"'2'"输出后为"2"
    7 123.0

     范例2:通过更改file参数打印内容到文件(Python 3.6.2 shell 环境)

    1 >>> with open(r'G:\temp.txt', 'w') as demo:  
    2 print(1, 2, 3, sep = ',', end = '\n', file = demo)
    3 
    4 
    5 >>>

      G盘下被新建txt文档’temp.txt’,其内容为:

      1,2,3

      line2

     

    范例3:格式化输出(Python 3.6.2 Shell 环境)

     1 >>> pi = 3.141592653
     2 >>> print('%f' % pi)                                   #默认愿长度为宽度,保留小数点后六位
     3 3.141593
     4 >>> print('%+f' % pi)                                  #显示正负号  
     5 +3.141593
     6 >>> print('%4f' % pi)                                  #宽度设小不会丢失精度
     7 3.141593
     8 >>> print('%10.3f' % pi)                               #宽度10,保留小数点后2位,右对齐
     9 *****3.142                                                #用*表示空格
    10 >>> print('%-10.3f' % pi)                              #宽度10,保留小数点后2位,左对齐
    11 3.142*****
    12 >>> print('%010.3f' % pi)                              #用0填充空白
    13 000003.142  
    14 >>> print('%e' % pi)                                    #科学计数法格式化输出
    15 3.141593e+00
    16 >>> print('%.2E' % (pi * 10000))
    17 3.14E+04
    18 >>> print('a%cc' % 'b')                                #单字符格式化输出
    19 abc
    20 >>> print('%c' % 'abc')                                #单字符格式输入多字符时的报错信息
    21 Traceback (most recent call last):
    22   File "<pyshell#12>", line 1, in <module>
    23     print('%c' % 'abc')
    24 TypeError: %c requires int or char
    25 >>> print('a%sd' % 'bc')                               #格式化输出字符串
    26 abcd
    27 >>> print('a%rd' % 'bc')                               #格式化输出字符串,保留“’’”
    28 a'bc'd
    29 >>>

     

    若您喜欢这篇文章请点个赞再走吧。欢迎转载,但请注明出处。由于水平有限,如果有不完善的地方还请在评论区中指正。有疑问欢迎在评论区中一起探讨。
  • 相关阅读:
    Spring Cloud Eureka的学习
    Maven环境配置
    Maven解决静态资源过滤问题
    Linux Desktop Entry文件配置解析
    iptables规则持久化
    Markdown学习总结
    输vim /etc/rc.d/init.d/mysqld 报错 …..localdomain.pid
    UE4 集成讯飞听写插件
    单机梦幻西游
    使用A*寻路小记
  • 原文地址:https://www.cnblogs.com/Dake-T/p/7376779.html
Copyright © 2011-2022 走看看