- Preface
- Introduction
- What Is A String
- What Are Escape Sequences
- More Ways to Span Lines
- String Concatenation
- More on Strings
- Review
Preface
This document is part of a series of online tutorial lessons designed to teach you how to program using the Python scripting language.
Something for everyone
Beginners start at the beginning, and experienced programmers jump in further along.
Learn to Program using Python: Lesson 1, Getting Started provides an overall description of this online programming course.
Introduction
I am taking it slow and easy for the first few lessons. My informal discussion is designed to familiarize you with the Python interactive programming environment while teaching you some important programming concepts at the same time.
This lesson provides an introduction to the use of strings.
What Is A String
The common interpretation of the word string in computer programming jargon is that a string is a sequence of characters that is treated as a unit. For example, a person’s first and last names are often treated as two different strings.
A person’s first name usually consists of several characters, and these characters are treated as a unit to produce a name.
What is a literal?
Perhaps the best way to describe a literal is to describe what it is not.
A literal is not a variable. In other words, the value of a literal doesn’t change with time as the program executes. You might say that it is taken at face value.
An expression using variables
For example, the following expression describes the sum of two variables named var1 and var2:
sum = var1 + var2
The result of this expression can vary depending on the values stored in var1 and var2 at the instant in time that the expression is evaluated.
An expression using literals
On the other hand, the following expression describes the sum of two literal numeric values:
sum = 6 + 8
No matter when this expression is evaluated, it will always produce a sum of 14.
String literals
Literal values can also be used for strings.
>>> “Dick Baldwin” ‘Dick Baldwin’ >>> ‘Dick Baldwin’ ‘Dick Baldwin’ >>> Dick Baldwin File “<stdin>”, line 1 Dick Baldwin ^ SyntaxError: invalid syntax >>> Figure 1 |
For example, the interactive code fragment in Figure 1 shows
- My name entered three times, in three different ways, on the interactive command line (highlighted in boldface)
- The output from the interpreter for each entry.
Oops!
The first two entries are valid string literals. As you can see, in the first two cases, the interpreter displays my name in the output.
Note that in the first two cases, my name is surrounded by either quotes (sometimes called double quotes) or apostrophes (sometimes called single quotes).
A syntax error
However, the third entry is not a valid string literal, and the interactive interpreter produced a syntax error message. In the third case, my name is not surrounded by either double quotes or single quotes, and that is what produced the error.
So, what is a valid string literal?
According to the Python Reference Manual, string literals can be enclosed in matching single quotes (‘) or double quotes (“).
This explains why the first two input lines in the above interactive code fragment were accepted and the third line produced an error.
Proper syntax
In the first line, my name was surrounded by matching double quotes. In the second input line, my name was surrounded by matching single quotes.
Bad syntax
However, in the third input line, my name was not surrounded by quotes of either type and this produced a syntax error.
More examples
Figure 2 shows two more examples of valid string literals with the input value highlighted in boldface.
>>> “””Dick Baldwin””” ‘Dick Baldwin’ >>> “””Dick … Baldwin””” ‘Dick |