From 4cf1328cc8a1eaadd02b898eee0a990c381a7654 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Wed, 19 Aug 2009 17:01:10 -0400 Subject: [PATCH] fixed slicing notes --- strings.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/strings.html b/strings.html index d2ec92c..ab77b39 100755 --- a/strings.html +++ b/strings.html @@ -287,9 +287,9 @@ experience of years. >>> a_string[18:] ' where your alphabet ends.'
    -
  1. You can get a part of a string, called a “slice”, by specifying two indices. The return value is a new string containing all the characters of the string, in order, starting with the first slice index (in this case a_string[0]), up to but not including the second slice index (in this case a_string[2]). +
  2. You can get a part of a string, called a “slice”, by specifying two indices. The return value is a new string containing all the characters of the string, in order, starting with the first slice index.
  3. Like slicing lists, you can use negative indices to slice strings. -
  4. Strings are zero-based, so a_string[0:3] returns the first three items of the string, starting at a_string[0], up to but not including a_string[3]. +
  5. Strings are zero-based, so a_string[0:2] returns the first two items of the string, starting at a_string[0], up to but not including a_string[2].
  6. If the left slice index is 0, you can leave it out, and 0 is implied. So a_string[:18] is the same as a_string[0:18], because the starting 0 is implied.
  7. Similarly, if the right slice index is the length of the string, you can leave it out. So a_string[18:] is the same as a_string[18:44], because this string has 44 characters. There is a pleasing symmetry here. In this 44-character string, a_string[:18] returns the first 18 characters, and a_string[18:] returns everything but the first 18 characters. In fact, a_string[:n] will always return the first n characters, and a_string[n:] will return the rest, regardless of the length of the string.