How the print () function does not wrap in Python

Source: Internet
Author: User
Tags flush

One, let the print () function does not break the line

In Python, the print () function defaults to newline. However, in many cases, we need output that does not wrap (for example, in an algorithmic race). So how do you do that in Python?

It's actually very simple. As long as you specify that the end parameter of the print () function is empty. (the default is ' \ n ')

For example:

1 Print ('helloWorld', end=') 2 Print ('!!! ')

The output is:

Two, print () function analysis

Of course, the print () function has not only the end parameter, but also several other parameters. Let's take a look at what these parameters do to the output respectively.

First look at the prototype of the print () function:

print(*objects, sep= ', end= ' \ n ', file=sys.stdout, flush=false)

  The following is excerpted from the official documentation:

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 is 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 is None, which means to use the default values. If No objects is given, print () would just write end.

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

Whether output is buffered are usually determined by file, and if the flush keyword argument is true, the Stream is forcibly flushed.

Changed in version 3.3: Added the flush keyword argument.

That is, print () converts objects to strings output to the stream, separated by Sep, ending with end.

Here are a few examples to take a concrete look at the function of the various parameters of the print () functions.

The first parameter objects not much to say, if do not know what to do can consider starting from scratch.

The second parameter, Sep, represents the character used when the objects parameter is concatenated, and the default is a space.

1 Print ('hello'World', sep='*' )

The output is:

The third parameter, end, represents the terminator after the output, and the default is line wrapping. The example is in front of.

The fourth parameter, file, indicates where the output is and the default is Sys.stdout. Must be a File-like object that has the Write method and cannot be in binary mode.

1 f = open ('print.txt'w')2  Print('hello'World', file=f)

  After the program finishes running, open the file to see:

The fifth parameter, flush, indicates whether to output immediately to the object specified by file. When True, the output is immediate and, when false, depends on the file object (which is generally not output immediately).

The above example, if added a pause, can be found that the data is not immediately written, only after F.close () is written. If the F.close () is not written, it is written after the program has finished running.

1f = open ('Print.txt','W')2 Print('Hello',' World', file=f)3s = input ('f.close ()? (y/n)')4 ifs = ='Y':5F.close ()

  If flush is true, it is immediately written.

1f = open ('Print.txt','W')2 Print('Hello',' World', File=f, flush=True)3s = input ('f.close ()? (y/n)')4 ifs = ='Y':5F.close ()

  Above is the python in the print () function of the analysis, in view of my limited level, there are irregularities, please also point out.

1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 4 #No Line break5 Print('Hello World', end='')6 Print('!!!')7 8f = open ('Print.txt','W')9F1 = open ('Print1.txt','W')Ten  One Print('Hello',' World') A Print('Hello',' World', sep='*') - Print('Hello',' World', file=f) - Print('Hello',' World', FILE=F1, flush=True) the  -s = input ('f.close ()? (y/n)') - ifs = ='Y': - f.close () +  - #if the input is N, pause here to see if the contents of the Print.txt are empty +s = input ()

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.