Description of Python print parameters

Source: Internet
Author: User

refer to Print's official documentation
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.
  • In Python, print prints the specified text to the screen by default, for example:

    >>>print(‘hello,world‘)
    hello world

  • The full format of print is print(objects,sep,end,file,flush) , with the following 4 optional parameters

    1. Sep
      Inserts the specified string between the output strings, which by default is a space, for example:
      >>>print("a","b","c",sep="**")
      a**b**c
    2. End
      The print specified string is added at the end of the output statement, and the default is newline (\ n), for example:
      >>>print("a",end="$")
      a$
      Print by default is a newline, that is, the output statement automatically switch to the next line, for Python3, if you want to implement the output does not wrap the function, then you can set end= "(Python2 can be added after the print statement," to implement the function of non-line wrapping)
    3. File
      The text is entered into the File-like object, which can be a file, a data stream, and so on, by default Sys.stdout
      >>>f = open(‘abc.txt‘,‘w‘)
      >>>print(‘a‘,file=f)
    4. Flush
      The flush value is true or FALSE, which defaults to Flase, which indicates whether the output statement is immediately entered into the object pointed to by the parameter file (by default, Sys.stdout), for example:
      >>>f = open(‘abc.txt‘,‘w‘)
      >>>print(‘a‘,file=f)
      You can see that the Abc.txt file is empty at this point and f.close() the content is written to the file only after execution.
      If you change to:
      >>>print(‘a‘,file=f,flush=True)
      You can immediately see the contents of the file

Description of Python print parameters

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.