Python in Sys.stdout, Sys.stdin

Source: Internet
Author: User

1. If you need better control of the output, and print does not meet the requirements, Sys.stdout,sys.stdin,sys.stderr is what you need.

2.sys.stdout and Print:

When you call print in Python, the sys.stdout.write (obj+ ' \ n ') is actually called

Print prints the required content to the console, and then appends a line break

The following two lines of code are equivalent:

Sys.stdout.write ('hello'\ n')print( ' Hello ')

3.sys.stdin and input

Sys.stdin.readline () will get all the standard input, including the ' \ n ' at the end, so the length is calculated with Len ' \ n ', but the result returned by input () does not include the end of the line feed ' \ n '.

Therefore, if you use Sys.stdin.readline () to get input, do not forget to remove the end of the line break, you can use the strip () function (Sys.stdin.readline (). strip (' \ n ') ) or Sys.stdin.readline () [:-1] These two methods remove line breaks.

4. Redirecting from console to file

The original sys.stdout points to the console, and if the object reference to the file is assigned to Sys.stdout, then print calls the Write method of the file object.

Python in Sys.stdout, Sys.stdin

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.