The difference between Sys.stdout.write and print >> sys.stdout in Python

Source: Internet
Author: User

Print >> sys.stdout in the form of a default output format of print, which equals print "%VALUE%"

Look at the English comment for the following code, which is the default Help for print

1234567891011121314151617 # coding=utf-8importsys, oslist1Display =[‘1‘‘2‘‘3‘]list2Display =[‘abc‘‘def‘‘rfs‘]whilelist2Display !=[]:    # 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.    # print 可以将值输出到指定的输出流(可以是文件句柄),若不指定,    # 则输出到stdout(标准输出)    # 一般我们使用的时候不加输出定向符“>>”到输出的file对象,本代码中对象是stdout    # 下面的print在stdout对象中每次输出两个值    print >> sys.stdout, list2Display.pop(), list1Display.pop()os.system( "pause")

Only the usage in python2.x is shown in the above, and print in 2.x cannot specify the end symbol as a different value, and the default is to output a "\ n", that is, it must be changed to the next line at a time. By the 3.x, print becomes a real function, and you can then specify the value of the end symbol arbitrarily, and you can add any value you want at the end of the output, instead of forcing a newline.

Therefore, in 2.x if you want to implement the output does not wrap, can only call the StdOut object's write method, the following is also an instance, because StdOut does not have the end of this symbol, the output will not be wrapped, so if you want to output multiple times, in the need to output the string object inside add "\ r , you can go back to the beginning of the line.

1234567 # coding=utf-8importsys, osimport timeforinrange(5):    time.sleep( .5)    sys.stdout.write( "File transfer progress :[%3d] percent complete!\r"% i )    sys.stdout.flush()

Experience, will the above "\ r" take off to try, is not a line and direct output? You got it. For a long time, Python will stay in the 2.x era.

The difference between running results is as follows:

-----------------------------------------------------------------

Network excerpt notes, if involved in the version, please contact the blogger delete.

The difference between Sys.stdout.write and print >> sys.stdout in Python

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.