Python
  Home arrow Python arrow Page 3 - Python: Stringing You Along
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PYTHON

Python: Stringing You Along
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2008-01-28


    Table of Contents:
  • Python: Stringing You Along
  • Making Text Appear Exactly As You Typed It
  • Other Ways to Span Text
  • String Operators

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Python: Stringing You Along - Other Ways to Span Text
    ( Page 3 of 4 )

    We saw above that you can use triple double quotes (or even triple single quotes if you want) to print your text exactly as it appears. You can also control how many lines (or not) your text will span. Below is some example code demonstrating this:


     

    #!/usr/local/bin/python


    print "This is how you turn multiple lines into one line:"

    print " "


    multiple = "Here is a whole bunch of text that will \

    fit on one line \

    when we are through"


    print multiple


    print "The following text will print on four lines: "

    print "Hello\nhow\nare\nyou?"


    print "We can also use parentheses to add several strings on separate lines into one:"


    three_strings = ("I like "

    " to eat"

    " pancakes")

    print three_strings


    print "Here is how we create a space between lines\n"

    print "By using an escape character"

    This results in:

      This is how you turn multiple lines into one line:

     

      Here is a whole bunch of text that will fit on one line when we are through

      The following text will print on four lines:

      Hello

      how

      are

      you?

      We can also use parentheses to add several strings on separate lines into one:

      I like to eat pancakes

      Here is how we create a space between lines

      By using an escape character

    Escaping

    Escaping allows you to print characters that are normally reserved for other purposes. To Python, a double quote normally signifies either the start of a string or the end of a string. If you want to print the quote so it shows up, you can encase it in another quote as described above (the preferred method) or you can use an escape character, as shown below:


     

    #!/usr/local/bin/python


    print "Here is an example of the single quote escape:"

    print 'It\'s okay'

    print "If you tried it without the escape, it would have returned an error.\n"

    print "The same with the double quote escape: "

    print "He said, \"Hi there.\" "

    print " "

    print "And of course you met our friend the newline escape\n\n"

    print "Here is the tab escape \t\t\t I'm over here!\n"

    print "And here is how you escape the backslash\\"

    This prints out:

      Here is an example of the single quote escape:

      It's okay

      If you tried it without the escape, it would have returned an error.

      The same with the double quote escape:

      He said, "Hi there."

     

      And of course you met our friend the newline escape

     

      Here is the tab escape             I'm over here!


      And here is how you escape the backslash\


    Here is a table showing some of the Escape characters:

     

    Character

    What it Does

     \

    Forces text on the following line to fit on the same line

     \\

    Lets you print a backslash

    \'

    Lets you print a single quote

    \”

    Lets you print a double quote

    \e

    The escape key

    \n

    Creates a space between your next sentence

    \t

    Creates a tabbed space in your string

    Getting Raw

    Sometimes you want to print text without having to worry about Python interpreting it as an escape code. This can happen if you type a path to a file on a server or your computer. You can use raw strings to accomplish this, by placing an r in front of the first quotation mark, like so:

    #!/usr/local/bin/python

    print r"C:python25text.py"

    This prints out:

      C:python25text.py



     
     
    >>> More Python Articles          >>> More By James Payne
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek