Strings, Indexing, and Slicing

Documentation

  1. Strings in the Python Tutorial
  2. Class str in the Python Standard Library
  3. Sequence Types in the Python Standard Library
  4. Strings in the Python Language Reference
  5. Sequence in the Python Glossary
  6. Subscription (i.e. [square brackets]) and slicing in the Python Language Refernce

A sequence is a type of variable that holds a series of zero or more values. For example, a string of characters is a type of variable that holds a series of zero or more characters. An example of a type of variable that is not a sequence is an int. An int holds only one value.

Length and indexing (i.e., subscripting)

The indices of a string start at 0. If the string has 16 characters, the indices therefore go up only to 15. You can also use negative numbers.

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
c o u n t e r c l o c k w i s e
-16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1

subscript.py

The value of s is counterclockwise
The type of s is <class 'str'>
The length of s is 16

The first character is c
The second character is o
The third character is u
The sixteenth character is e

The last character is e
The next-to-last character is s
The third-from-last character is i

For subscripting, see “Strings can be indexed” in Strings; Subscription; Common Sequence Operations.

Things to try

  1. Print a slice. See “slicing” in Strings.
    print(s[0:7])    #counter
    print(s[7:12])   #clock
    print(s[12:16])  #wise
    
    counter
    clock
    wise
    
  2. Omit one or both of the numbers next to the colon:
    print(s[:7])     #counter          (the first 7 characters)
    print(s[7:])     #clockwise        (all but the first 7 characters)
    print(s[:])      #counterclockwise (all the characters)
    print(s)         #counterclockwise (all the characters)
    
    counter
    clockwise
    counterclockwise
    counterclockwise
    

    Why did they invent the last example ([:])? See “If you need to modify” in for, and the subtlety. See also the copying example in Sort.

  3. Three ways to print wise.
    print(s[12:16])  #wise
    print(s[12:])    #wise         (all but the first 12 characters)
    print(s[-4:])    #wise         (the last 4 characters)
    
    print(s[:-4])    #counterclock (all but the last 4 charcters)
    
    wise
    wise
    wise
    counterclock
    
  4. Use two colons to specify a stride or step.
    print(s[12:])       #wise
    print(s[12:16])     #wise
    print(s[12:16:1])   #wise (the stride is 1)
    
    print(s[15:11:-1])  #esiw (the stride is -1)
    print(s[-1:-5:-1])  #esiw (the stride is -1)
    print(s[-1::-1])    #esiwkcolcretnuoc (the stride is -1)
    print(s[::-1])      #esiwkcolcretnuoc (the stride is -1)
    print(reversed(s))  #esiwkcolcretnuoc
    
    wise
    wise
    wise
    esiw
    esiw
    esiwkcolcretnuoc
    esiwkcolcretnuoc
    esiwkcolcretnuoc
    
  5. A stride greater than 1.
    letters = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
    print(letters[::2])
    print(letters[1::2])
    
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    abcdefghijklmnopqrstuvwxyz
    

    This 9-character string is a 3 × 3 playing board for tic-tac-toe. Here are five ways to create it. All five statements create exactly the same string.

    board = "012345678"
    
    board = "012" + "345" + "678"
    
    board = \
          "012" \
        + "345" \
        + "678"
    
    board = ("012" + "345" + "678")
    
    board = (
          "012"
        + "345"
        + "678")
    

    Now that we have created the string, here’s how to print the eight straight lines that will win the game.

    #The 3 rows.
    print(board[:3])    #012
    print(board[3:6])   #345
    print(board[6:])    #678
    
    #The 3 columns.  Count by 3's.
    print(board[::3])   #036
    print(board[1::3])  #147
    print(board[2::3])  #258
    
    #The 2 diagonals.
    print(board[::4])   #048 upper left to lower right
    print(board[2:8:2]) #246 upper right to lower left
    
    if board[:3] == "XXX":   #Is the top row all X's?
    
    if board[:3] == 3 * "X":
    
    #Moving average.  prices is a list.
    #Compute the average of 3 prices: prices[i-1], prices[i], prices[i+1]
    
    if sum(prices[i-1:i+2]) / 3 >= 100:
    
  6. Let the user select a character.
    while True:
        i = input("What is the index of the character you want to see? ")
        i = int(i)
        print(s[i])
    
    What is the index of the character you want to see? 12
    w
    What is the index of the character you want to see? 14
    s
    What is the index of the character you want to see? 16
    Traceback (most recent call last):
      File "/Users/myname/python/subscript.py", line 28, in <module>
        print(s[i])
    IndexError: string index out of range
    
  7. Catch the IndexError exception.
    while True:
        i = input("What is the index of the character you want to see? ")
        i = int(i)
    
        try:
            print(s[i])
        except IndexError:
            print(f"Sorry, index must be in the range {-len(s)} to {len(s) - 1} inclusive.")
    
    What is the index of the character you want to see? 16
    Sorry, index must be in the range -16 to 15 inclusive.
    What is the index of the character you want to see? 14
    s
    What is the index of the character you want to see? etc.
    
  8. We caught the ValueError exception here.
    while True:
        i = input("What is the index of the character you want to see? ")
    
        try:
            i = int(i)
        except ValueError:
            print("Sorry, index must be an integer.")
            continue  #Go back up to the keyword while.
    
        try:
            print(s[i])
        except IndexError:
            print(f"Sorry, index must be in the range {-len(s)} to {len(s) - 1} inclusive.")
    
    What is the index of the character you want to see? zero
    Sorry, index must be an integer.
    What is the index of the character you want to see? 0
    c
    What is the index of the character you want to see?  etc.
    
  9. Similarly, we caught the EOFError exception here.
    while True:
        try:
            i = input("What is the index of the character you want to see? ")
        except EOFError:
            sys.exit(0)
    
        try:
            i = int(i)
        except ValueError:
            print("Sorry, index must be an integer.")
            continue
    
        try:
            print(s[i])
        except IndexError:
            print(f"Sorry, index must be an integer in the range {-len(s)} to {len(s) - 1} inclusive.")
    
    What is the index of the character you want to see? 0
    c
    What is the index of the character you want to see? control-d on macOS, control-z enter on PC
    (the script exits)
    
  10. Recognize a fraction and complain about it.
    while True:
        try:
            i = input("What is the index of the character you want to see? ")
        except EOFError:
            sys.exit(0)
    
        try:
            _ = float(i)
        except ValueError:
            print("Sorry, index must be a number.")
            continue
    
        try:
            i = int(i)
        except ValueError:
            print("Sorry, index must be an integer.")
            continue
    
        try:
            print(s[i])
        except IndexError:
            print(f"Sorry, index must be an integer in the range {-len(s)} to {len(s) - 1} inclusive.")
    
    What is the index of the character you want to see? three
    Sorry, index must be a number.
    What is the index of the character you want to see? 3.5
    Sorry, index must be an integer.
    What is the index of the character you want to see? 3
    n
    What is the index of the character you want to see?  etc.
    
  11. Change the lowercase w to uppercase.
    s = "counterclockwise"
    s[12] = "W"
    print(s)
    
    Traceback (most recent call last):
      File "/Users/myname/python/index.py", line 10, in <module>
        s[12] = "W"
    TypeError: 'str' object does not support item assignment
    

    We therefore say that a string is immutable. To accomplish what we are trying to do, we have to build a new string:

    s = "counterclockwise"
    s = s[:12] + "W" + s[13:]   #Paste together a new s.
    #s = f"{s[:12]}W{s[13:]}"   #another way to do the same thing.
    print(s)
    
    counterclockWise
    
    See List of numbers for another way to make the w uppercase.