Python Programming for the Absolute Beginner, 3rd Edition

Indexing is a useful technique, but you aren't restricted to copying just one element at a time from a sequence. You can make copies of continuous sections of elements (called slices). You can copy (or slice) one element (just like indexing) or part of a sequence (like, say, the middle three elements). You can even create a slice that is a copy of the entire sequence. So, for strings, that means you can grab anything ranging from a single character, to a group of consecutive characters, to the entire string.

Introducing the Pizza Slicer Program

The Pizza Slicer program lets you slice the string "pizza" any way you want. It's a great, interactive way to help you understand slicing. All you do is enter the starting and ending positions of the slice, and the program displays the results. Figure 4.9 shows off the program.

Figure 4.9: Fresh, hot slices of "pizza", made just the way you asked. The program also offers a "cheat sheet" so you can visualize how a slice will be created.

Here's the code:

# Pizza Slicer # Demonstrates string slicing # Michael Dawson - 1/27/03 word = "pizza" print \ """ Slicing 'Cheat Sheet' 0 1 2 3 4 5 +—--+—--+—--+—--+-—-+ | p | i | z | z | a | +—-+—-+—-+—-+—-+ -5 -4 -3 -2 -1 """ print "Enter the beginning and ending index for your slice of 'pizza'." print "Press the enter key at 'Begin' to exit." begin = None while begin != "": begin = (raw_input("\nBegin: ")) if begin: begin = int(begin) end = int(raw_input("End: ")) print "word[", begin, ":", end, "]\t\t", print word[begin:end] raw_input("\n\nPress the enter key to exit.")

Introducing None

Before you get to the code about slicing, take a look at this line which introduces a new idea:

begin = None

The line assigns a special value, called None, to begin. None is Python's way of representing nothing. None makes a good placeholder for a value. It also evaluates to false when treated as a condition. I used it here because I wanted to initialize begin for use in the while loop condition.

Understanding Slicing

Creating a slice is similar to indexing. But instead of using a single position number, you supply a starting position and ending position. Every element between the two points becomes part of the slice. Figure 4.10 shows a way to look at slicing end point numbers for the string "pizza". Notice that it's a slightly different numbering system than the index numbering in Figure 4.6.

Figure 4.10: An example of slicing end point numbers for the string "pizza". You can use any combination of positive and negative end points for your slice.

To specify the end points of a slice, include both in brackets, separated by a colon. Here's a quick interactive session to show you what I mean:

>>> word = "pizza" >>> print word[0:5] pizza >>> print word[1:3] iz >>> print word[-4:-2] iz >>> print word[-4:3] iz

word[0:5] returns the entire string because all its characters are between those two end points. word[1:3] returns the string "iz" because those two characters are between the end points. Just like with indexing, you can use negative numbers. word[-4:-2] also produces the string "iz" because those characters are between the two negative positions. You can also mix and match positive and negative end points. This works just like creating any other slice; the elements between the two position numbers will be in the slice. So, word[-4:3] also produces the string "iz", because they are the two characters between those two end points.

TRAP

If you create an "impossible" slice, where the starting point is bigger than the ending point, like word[2:1], you won't cause an error. Instead, Python will quietly return an empty sequence. For strings, that means you'll get the empty string. So be careful, because this is probably not the kind of result you're after.

Creating Slices

Inside the loop of program Pizza Slicer, the program prints the syntax for creating a slice based on the beginning and ending positions the user entered, through the following line:

print "word[", begin, ":", end, "]\t\t",

Then, the program prints the actual slice using the variables begin and end:

print word[begin:end]

Using Slicing Shorthand

Although you can get every possible slice by specifying two numbers, there are a few slicing shortcuts you can use. You can omit the beginning point for the slice to start the slice at the beginning of the sequence. So, given that word has been assigned "pizza", the slice word[:4] is exactly the same as word[0:4]. You can omit the ending point so that the slice ends with the very last element. So, word[2:] is just shorthand for word[2:5]. You can even omit both numbers to get a slice that is the entire sequence. So, word[:] is shorthand for word[0:5].

Here's an interactive session to back up this proposition:

>>> word = "pizza" >>> word[0:4] 'pizz' >>> word[:4] 'pizz' >>> word[2:5] 'zza' >>> word[2:] 'zza' >>> word[0:5] 'pizza' >>> word[:] 'pizza'

TRICK

If there's one bit of slicing shorthand you should remember, it's that [:] returns a complete copy of a sequence. As you program, you'll find you may need to make a copy of a sequence, and this is a quick and efficient way to do just that.

Категории