• No results found

The String Data Type

In document Python Programming for Starters (Page 108-111)

Sequences: Strings, Lists, and Files

5.1 The String Data Type

So far, we have been discussing programs designed to manipulate numbers and graphics. But you know that computers are also important for storing and operating on textual information. In fact, one of the most common uses for personal computers is word processing. This chapter focuses on textual applications to introduce some important ideas about how text is stored on the computer.

You may not think that word-based applications are all that exciting, but as you’ll soon see, the basic ideas presented here are at work in virtually all areas of computing, including powering the the World-Wide Web.

Text is represented in programs by the string data type. You can think of a string as a sequence of characters. In Chapter 2 you learned that a string literal is formed by enclosing some characters in quotation marks. Python also allows strings to be delimited by single quotes (apostrophes).

99

100 Chapter 5. Sequences: Strings, Lists, and Files There is no difference; just be sure to use a matching set. Strings can also be saved in variables, just like any other data. Here are some examples illustrating the two forms of string literals:

>>> str1 = "Hello"

>>> str2 = ’spam’

>>> print(str1, str2) Hello spam

>>> type(str1)

<class ’str’>

>>> type(str2)

<class ’str’>

You already know how to print strings. You have also seen how to get string input from users.

Recall that the input function returns whatever the user types as a string object. That means when you want to get a string, you can use the input in its “raw” (non evaled) form. Here’s a simple interaction to illustrate the point:

>>> firstName = input("Please enter your name: ") Please enter your name: John

>>> print("Hello", firstName) Hello John

Notice how we saved the user’s name with a variable and then used that variable to print the name back out again.

So far, we have seen how to get strings as input, assign them to variables, and how to print them out. That’s enough to write a parrot program, but not to do any serious text-based computing. For that, we need some string operations. The rest of this section takes you on a tour of the more important Python string operations. In the following section, we’ll put these ideas to work in some example programs.

What kinds of things can we do with strings? For starters, remember what a string is: a sequence of characters. One thing we might want to do is access the individual characters that make up the string. In Python, this can be done through the operation of indexing. We can think of the positions in a string as being numbered, starting from the left with 0. Figure 5.1 illustrates with

H e l l o B o b

0 1 2 3 4 5 6 7 8

Figure 5.1: Indexing of the string "Hello Bob"

the string “Hello Bob.” Indexing is used in string expressions to access a specific character position in the string. The general form for indexing is <string>[<expr>]. The value of the expression determines which character is selected from the string.

Here are some interactive indexing examples:

5.1. The String Data Type 101

Notice that, in a string ofn characters, the last character is at position n − 1, because the indexes start at 0. This is probably also a good time to remind you about the difference between string objects and the actual printed output. In the interactions above the Python shell shows us the value of strings by putting them in single quotes; that’s Python’s way of communicating to us that we are looking at a string object. When we actually print the string, Python does not put any quotes around the sequence of characters. We just get the text contained in the string.

By the way, Python also allows indexing from the right end of a string using negative indexes.

>>> greet[-1]

’b’

>>> greet[-3]

’B’

This is particularly handy for getting at the last character of a string.

Indexing returns a string containing a single character from a larger string. It is also possible to access a contiguous sequence of characters or substring from a string. In Python, this is accom-plished through an operation called slicing. You can think of slicing as a way of indexing a range of positions in the string. Slicing takes the form <string>[<start>:<end>]. Both start and endshould be int-valued expressions. A slice produces the substring starting at the position given by start and running up to, but not including, position end.

Continuing with our interactive example, here are some slices:

>>> greet[0:3]

The last three examples show that if either expression is missing, the start and end of the string are the assumed defaults. The final expression actually hands back the entire string.

102 Chapter 5. Sequences: Strings, Lists, and Files

Operator Meaning

+ Concatenation

* Repetition

<string>[ ] Indexing

<string>[ : ] Slicing len(<string>) Length

for<var> in <string> Iteration through characters Table 5.1: Python string operations.

Indexing and slicing are useful operations for chopping strings into smaller pieces. The string data type also supports operations for putting strings together. Two handy operators are concate-nation (+) and repetition (*). Concateconcate-nation builds a string by “gluing” two strings together.

Repetition builds a string by multiple concatenations of a string with itself. Another useful func-tion is len, which tells how many characters are in a string. Finally, since strings are sequences of characters, you can iterate through the characters using a Python for loop.

Here are some examples of various string operations:

>>> "spam" + "eggs"

’spameggs’

>>> "Spam" + "And" + "Eggs"

’SpamAndEggs’

>>> 3 * "spam"

’spamspamspam’

>>> "spam" * 5

’spamspamspamspamspam’

>>> (3 * "spam") + ("eggs" * 5)

’spamspamspameggseggseggseggseggs’

>>> len("spam") 4

>>> len("SpamAndEggs") 11

>>> for ch in "Spam!":

print(ch, end=" ") S p a m !

These basic string operations are summarized in Table 5.1.

In document Python Programming for Starters (Page 108-111)