Home » Python » Zero basics, python-10.4 printing

Zero basics, python-10.4 printing

this chapter talks about printing. In Python, printing is closely related to the concept of file and stream

1. file object method

is similar to the file write method, where print prints objects to the stdout stream, then adds some automatic formatting, and does not have to change objects to string

in the printing process

2. standard output stream

consists of the standard input stream and the error stream that form the data connection 3 created by the script when it is started.

3. call format

print (objects, sep='', end='', file=sys.stdout)

objects refers to multiple objects,

sep refers to what characters are inserted between objects.

end means what ends with

file refers to the place where the text is sent.

>>> print ('); abc' sep=' ' end=' t')
> > > Print (' abc', ' efg' sep=' ',,; end=' t')
ABC efgt
> > > Print (' aaa', ' bbb' ' ccc' ',,; ddd' sep=' sep',, end=' end')
> > >

4. automated flow redirection

, we'll change the print to the text write method

>>> import sys
> > > temp=sys.stdout
> > > sys.stdout =open (' 123.txt' ' a',)
> > > (' abcdefg' print)
Print (123456).
Sys.stdout (.Close).
> > > sys.stdout =temp
> > > (' --------------' print)
> > > Print (open (' 123.txt'.Read) ())
One hundred and twenty-three thousand four hundred and fifty-six
One hundred and twenty-three thousand four hundred and fifty-six
> > >

In the example above, we redirect the standard output stream and put its interface into a file,

then, we use the print method instead of the write method,

here it is. Thank you,


Click jump, zero basics, python-, directory,

copyright notice: This article is original article for blogger. Without permission from blogger,.

must not be reprinted