zoukankan      html  css  js  c++  java
  • Python3 print()函数sep,end,file参数用法练习

    来自builtins.py:
    def print(self, *args, sep=' ', end='\n', file=None): # known special case of print

    """
    print(value, ..., sep=' ', end='\n', 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.
    """
    pass


    使用1,练习sep的用法:
    print("file\n","abc","bcd","fff\n","poi")
    print("-------------")
    print("file\n","abc","bcd","fff\n","poi",sep='')
    print("-------------")
    print("file\n","abc","bcd","fff\n","poi",sep=' ')
    print("-------------")
    
    
    其结果为:
    file
     abc bcd fff
     poi
    -------------
    file
    abcbcdfff
    poi
    -------------
    file
     abc bcd fff
     poi
    -------------

    使用2,练习end的用法:

    1 print("file\n","abc","fff",sep='#########\n',end='')
    2 print("--------------------")
    3 print("file\n","abc","fff",sep='#########\n',end='\n')
    4 print("--------------------")
    5 print("file\n","abc","fff",sep='#########\n')
    6 print("--------------------")
    其结果为:
    file
    #########
    abc#########
    fff--------------------
    file
    #########
    abc#########
    fff
    --------------------
    file
    #########
    abc#########
    fff
    --------------------
    使用3,练习file的用法:
    1 with open('abc.txt','w') as f:
    2     print("file\n","abc","fff",sep='#########\n',end='',file=f)
    其结果:
    将输出写到了文件abc.txt。
    abc.txt的内容如下:
    file
    #########
    abc#########
    fff

     注意文件末行没有换行符



  • 相关阅读:
    重建二叉树
    从尾到头打印链表
    合并两个数组
    替换空格
    二维数组中的查找
    机器学习中的 precision、recall、accuracy、F1 Score
    rand、randi和randn的区别?
    使用自己的数据集训练和测试"caffenet"
    caffe环境的搭建(Ubuntu14.04 64bit,无CUDA,caffe在CPU下运行)
    Java 并发工具包 java.util.concurrent 用户指南
  • 原文地址:https://www.cnblogs.com/owasp/p/5372476.html
Copyright © 2011-2022 走看看