next up previous contents
Next: Special Characters and Raw Up: String Data Previous: String Data   Contents

String Constants

Strings are a collection of characters which are stored together to represent arbitrary text inside a python program. You can create a string constant inside a python program by surrounding text with either single quotes ('), double quotes ("), or a collection of three of either types of quotes (''' or """). In the first two cases, the opening and closing quotes must appear on the same line in your program; when you use triple quotes, your text can span as many lines as you like. The choice of which quote symbol to use is up to you - both single and double quotes have the same meaning in python.

Here are a few examples of how to create a string constant and assign its value to a variable:

name = 'Phil'
value = "$7.00"
helptext = """You can create long strings of text
spanning several lines by using triple quotes at
the beginning and end of the text"""
When the variable helptext is printed (using the print command; Section 5.1), it would display as three lines, with the line breaks at the same points as in the triple-quoted text.

You can also create strings by reading input from a file (Section 5.4.1), or by concatenating smaller strings (Section 2.4.1).


next up previous contents
Next: Special Characters and Raw Up: String Data Previous: String Data   Contents
Phil Spector 2003-11-12