Lean Python: Learn Just Enough Python to Build Useful Tools by Paul Gerrard

Lean Python: Learn Just Enough Python to Build Useful Tools by Paul Gerrard

Author:Paul Gerrard [Paul Gerrard]
Language: eng
Format: epub
Publisher: Apress
Published: 2016-11-10T22:00:00+00:00


Getting input from and writing output to disk files.

Displaying Output

You’ve seen the print() function1 quite a few times already. The most common way of getting output from a program is to use the print() statement. Print is a function that takes as its arguments the items to be displayed. Optionally, you can also define a separator that is placed between the displayed items and a line terminator value that can replace a newline. The function call looks like this:

print(arg1,arg2,arg3...,sep=' ',end='\n')

Here are some examples of the print() function in use.

>>> boy="Jack"

>>> girl="Jill"

>>> print("Hello World!")

Hello World!

>>> print(boy,'and',girl,'went up the hill')

Jack and Jill went up the hill

It is common to use the string formatting feature.

>>> print('%d plus %d makes %d' % (3, 7, 10))

3 plus 7 makes 10

You can suppress the trailing newline by setting the end argument to an empty string (or something else).

>>> #

>>> # the end= argument defaults to '\n'

>>> # if you change it, there won't be a newline

>>> #

>>> print('one...','two...','three',end='')

one... two... three>>> # note the >>> prompt

The string separator defaults to a single space but can be changed or suppressed by setting it to an empty string.

>>> #

>>> # the sep= argument defaults to a space ' '

>>> # but you can change it, for example...

>>> #

>>> print('one...','two...','three',sep='***')

one...***two...***three



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.