Printing a Chunk Of Words

Rick Johnson rantingrickjohnson at gmail.com
Mon Sep 25 20:28:16 EDT 2017


On Monday, September 25, 2017 at 7:15:41 PM UTC-5, Cai Gengyang wrote:
> """
>      Boolean Operators
> ------------------------      
> True and True is True
> True and False is False
> False and True is False
> False and False is False
> 
> True or True is True
> True or False is True
> False or True is True
> False or False is False
> 
> Not True is False
> Not False is True
> 
> """
> 
> If I simply want to print a chunk of words and a paragraph
like the above, what command should I use ?

Well, being that what you have there is a "multi-line string
literal", then you can pass it the print function (py3).

    ## Begin: Python2.x interactive session ##
    >>> print """this is my
    totally awesome multi-line
    string"""
    this is my
    totally awesome multi-line
    string
    ## End: Python2.x interactive session ##

Although, i would assign a name to it first, as passing
around literals is not practical.

    ## Begin: Python2.x interactive session ##
    >>> s = """this is my
    totally awesome multi-line
    string"""
    >>> print s
    this is my
    totally awesome multi-line
    string
    ## End: Python2.x interactive session ##
    
However, if your intention is to evaluate each line of the
"paragraph" (as you called it), then you have to do a little
more work.

    ## Begin: Python2.x interactive session ##
    >>> for line in s.splitlines(True):
    ...     print repr(line)
    'this is my\n'
    'totally awesome multi-line\n'
    'string'

Which is a start...



More information about the Python-list mailing list