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

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 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? 
PYTHON

Python: Stringing You Along
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    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


       · Thanks for dropping by to read my article on Python strings. In this episode we...
       · Thanks for the article, but the text spanning examples are broken. The backslashes...
       · Hey, thanks for catching that; our content system sometimes gobbles up our...
       · I fixed the article as well. Thanks.-James Payne
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway