Home arrow Python arrow Page 3 - Python: Stringing You Along

Other Ways to Span Text - Python

If you have ever programmed before, or are a ninety-year-old lady, or a cat, then you know how useful strings are. If you have been reading these articles, you will know that a string is a character, a sentence, a paragraph, a book, etc. If you haven't been reading these articles, then you're missing an important part of your education as a programmer. Don't keep yourself in the dark; start reading!

TABLE OF CONTENTS:
  1. Python: Stringing You Along
  2. Making Text Appear Exactly As You Typed It
  3. Other Ways to Span Text
  4. String Operators
By: James Payne
Rating: starstarstarstarstar / 5
January 28, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- 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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: