As you know, in Python a string data type can hold a character, word, phrase, or longer stretches of text. You have learned how to gather string input from the user and print formatted string messages to the screen. In this chapter, we'll take a closer look at how strings are formed and explore some of the Python functions that allow you to search, extract and transform string content. Let's begin by closely examining the characters that make up a string value.

Strings as Lists of Characters
A Python string is made of a series of individual characters, stored back-to-back in a certain order. You can visualize a string just like a Python list, where each element in the list is simply one character like "G". Just like list elements, string characters can be identified by an integer index that starts at 0 and goes up by 1 for each additional character in the string. For example, the string "Giraffe" has 7 characters, so the matching index values are 0 through 6.

Giraffe string with matching index values

Remember, you can read individual list elements using the list variable name, square brackets, and the numeric index of the element. The same type of access can be used to get individual letters of a string! In the example below, we initialize a string to hold the value "Giraffe". We then use the variable name, square brackets, and index values to read some of the characters from the string and print them to the screen. Can you predict which three characters will be displayed?

Try It Now

Of course, you need to be careful not to use an index value that greater than or equal to the string length. Try editing the code above and using the numeric index 7 inside the square brackets. When you run that code, you will see an IndexError exception at run-time.

Traceback (most recent call last):
File "code1.py", line 3, in
letter0 = myString[7]
IndexError: string index out of range
Strings are Immutable
In Python, once you create a string value, it is immutable, which means that object can't change. So, while you can update individual list elements by index, don't try to do the same thing to Python strings! The code below will throw a run-time exception when you try to set a character at a specific index equal to some other character.

myString = "Giraffe"
myString[0] = "X" # try to create "Xiraffe" - will not work!
Copy
This does NOT mean that you can never change a string variable, of course. You can always assign a completely different string to a variable. In that case, you are replacing one string value with another, which is perfectly fine.

myString = "Giraffe"
myString = "Xiraffe" # replace one string value with another - will always work
Copy
String Length with len()
You have already used the len() function to tell you how many elements are in a list or tuple. This function works the same way on strings and will tell you how many characters are in the string. Try experimenting with some different values for myString in the example below; do you get the expected length printed each time?

Try It Now

Reminder - String Concatenation
As you know from an earlier chapter, strings can easily be combined with concatenation. This simply means you use the plus sign (+) to join together two smaller strings to make one larger string. The two smaller strings are glued end-to-end without any automatic spacing. Can you change the code below so the first and last names are printed with a space between them? You could include a space at the end of "Daffy" or at the beginning of "Duck" or add a space " " between the firstname and lastname in the print() statement.

Try It Now

Escape Sequences
It's easy to create strings with simple alphanumeric characters like "a"-"z" or "0"-"9". Simply type the characters you want in-between double quotes. However, if the string you want to build actually contains a double-quote, what would happen? Consider the phrase below from Edgar Allen Poe's poem, "The Raven":

Quoth the Raven, "Nevermore."
Let's try to print this to the screen as a Python string. We might first try to write code as follows:

print("Quoth the Raven, "Nevermore."") # COMPILE ERROR - Will not work
Copy
However, this won't work! Python uses double quotes to mark the beginning and end of strings. So, Python would recognize the characters between the first two double quotes as a string, followed by some unrecognized and invalid characters, and finally find another (empty) string at the end.

"Quoth the Raven, "Nevermore.""

To successfully create a string that includes special characters like double quotes, you need to use escape sequence inside the double-quoted string. An escape sequence is actually a combination of characters that, when present, represent a single character that might otherwise not be easy to type into a Python string.

Most escape sequences start with a backslash (\). To place a double-quote inside a string, you would write a backslash followed by the double quote (\"). Python will see this sequence as a single double-quote character. Let's fix our example so the quote will be printed correctly on the screen. Try it and verify the results.

Try It Now

Python defines escape sequences for many characters that can't be easily typed into a string within your code. Tabs, the "Enter" key, and similar special characters all have escape sequences. For example, the sequence "\n" is a string of length 1 that holds the "new line" character. This causes your console output to skip down to the next line. The print() function will automatically add a "\n" to the end of your output message (unless you tell it otherwise) - that's why each of your print() messages normally appear on a different line!

The table below describes some common Python escape sequences.
Escape Character

Description

\n

New Line

\r

Carriage Return

\t

Horizontal Tab

\’

Single Quote (‘)

\”

Double Quote (“)

\\

Backslash (\”)

Notice that the backslash character itself (\) has an escape sequence! Why is this needed? Python will normally assume that a backslash inside a string is the beginning of an escape sequence. If it is not, then you need to use an escape sequence just to add a normal backslash. The example below shows the right and wrong ways to create the string "\I like backslashes\".

print("\I like backslashes\") # Will not work - \I and \" treated as escape sequences
print("\\I like backslashes\\") # Correct approach with escaped backslashes
Copy
Take some time to experiment with escape sequences in the example code below. You can run the code first to see sample output from each sequence, then change the strings on your own.

Try It Now

Notice that the new line character (\n) and the carriage return (\r) seem to have the same effect. Historically, different operating systems have used either a new line (\n) alone or a combination of carriage return and new line (\r\n) to mark the end of lines in a text file. The print() statement will use either character to advance to the next line in a Python console. However, if you are actually writing data to a file that will be used on a specific operating system, you will want to be sure to match the End-of-Line (EOL) marker required by that operating system.

ask some multiple choice questions.

1. How are individual characters in a string identified in Python?

A. By using curly brackets and index values
B. By using square brackets and index values
C. By using parentheses and index values
D. By using angle brackets and index values

2. Which of the following statements about Python strings is true?
A. Strings are mutable and can be changed easily.
B. Strings are made of a series of individual integers.
C. Strings can only hold single characters, not longer stretches of text.
D. Strings are immutable and cannot be changed in place.

3. Which function in Python can be used to determine the length of a string?
A. size()
B. count()
C. length()
D. len()

4. What is the escape sequence for a new line character in Python?
A. \t
B. \n
C. \r
D. \\

5. When including a single quote or double quote character in a Python string, what escape sequence should be used?
A. \'
B. \"
C. \\\'
D. \\\"