1. Home
  2. Computing & Technology
  3. Python

Beginning Python: Data Types

By Al Lukaszewski, About.com

4 of 10

Strings - Part 2

Because a string represents words and numbers in the format they are assigned, Python uses quotes to indicate their value. Trying to assign a string value without quotes looks like you are trying to assign the value of one string to another. If the string does not yet exist, Python will tell you so. To assign a value to a string, one uses the following template:

<name of string> = '<value of string>'

Examples of variable assignments are:

gettysburg = "four score and seven years ago..."
writ = 'habeas corpus'
Catch22 = """no win situation"""
postcode = '''91101-2509'''

"Wait a minute!", I hear you say, "You said that Python uses quotes to offset string values. You used single quotes in the template and then used single, double and triple quotes in the examples. Which is right?" The fact of the matter is that Python allows all of them. You get to choose which you want to use. Python does not care. All Python asks is that you close the value with the same kind that you opened it (otherwise, Python will not know where you mean the quotes to be part of the value or to signify the end of the string). If you begin a value with double quotes, you must end it with double quotes. The following are therefore WRONG:

name = 'Joanna Sebastian Bach"
body = "Sargasso See'
city = """Ed in burgh'''

4 of 10

Explore Python

More from About.com

  1. Home
  2. Computing & Technology
  3. Python

©2008 About.com, a part of The New York Times Company.

All rights reserved.