22. Text string indices

Text strings are made up of individual characters that can be obtained using indices. A text string index is a number within two opening and closing [] brackets:

>>> lang = 'Python'
>>> lang[0]
    'P'
>>> lang[1]
    'y'
>>> lang[2]
    't'
>>> lang[3]
    'h'

The first character 'P' has zero index 'lang[0]'. This is a convention used by many other programming languages ​​such as the C language or the JavaScript language. The first time you learn it, it is not easy to get used to it, but it is necessary to be able to write correct code.

Negative indices start at the end of the text string:

>>> lang[-1]
    'n'
>>> lang[-2]
    'o'
>>> lang[-3]
    'h'
>>> lang[-4]
    't'

Indices out of range cause a program error:

>>> lang[20]
    Traceback (most recent call last):
      File "<pyshell#1>", line 1, in <module>
        lang[20]
    IndexError: string index out of range

Slices or slicers

Indices can be written to retrieve multiple characters from a string, using the colon ':' character to separate start from end in an index:

>>> lang = 'Python'
>>> lang[0:3]
    'Pyt'
>>> lang[2:5]
    'tho'
>>> lang[1:-2]
    'yth'
>>> lang[-3:-2]
    'h'

The following schematic can help you better understand how indexes and slices work:

|   +---+---+---+---+---+---+
|   | P | y | t | h | o | n |
|   +---+---+---+---+---+---+
|   0   1   2   3   4   5   6
|  -6  -5  -4  -3  -2  -1

An index will take the letter to its right.

A slice will take the characters between the two numbers. For example, [1:-2] will take all characters between indices 1 and -2 'yth'

|   +---+---+---+---+---+---+
|   | P | y | t | h | o | n |
|   +---+---+---+---+---+---+
|       1          -2

Slice numbers can also be left unwritten and this is equivalent to saying take all characters from the start or take all characters to the end of the string:

>>> lang[:3]
    'Pyt'
>>> lang[3:]
    'hon'
>>> lang[:]
    'Python'

Slices with out-of-range indices do not produce an error, they just return an empty string:

>>> lang[20:30]
    ''

Loops with text strings

A for loop can take the characters of a text string one by one:

>>>  for c in 'Hola, mundo':
...      print(c)
...
...
     H
     o
     l
     a
     ,

     m
     u
     n
     d
     o

Exercises

  1. Write a function that prints one by one all the characters of a text string that we pass to it as an argument.

    Call the function twice with two different text strings.

  2. Write a function that accepts a string as an argument and that prints the string bit by bit from one character to all characters.

    Example:

    >>> letrero('En un lugar de la Mancha')
        E
        En
        En
        En u
        En un
        En un
        En un l
        En un lu
        ...
        ...
    
  3. Modify the above function so that it prints the text string starting from the right, character by character, until ending:

    >>> letrero_inverso('En un lugar de la Mancha')
        a
        ha
        cha
        ncha
        ancha
        Mancha
         Mancha
        a Mancha
        la Mancha
        ...
        ...
    
  4. Write a function that accepts a string as an argument and prints groups of five characters of the text from the beginning of the string to the end:

    >>> letrero_corto('En un lugar de la Mancha')
            E
           En
          En
         En u
        En un
        n un
         un l
        un lu
        n lug
         luga
        lugar
        ugar
        gar d
        ar de
        r de
         de l
        de la
        ...
        ...
        ancha
        ncha
        cha
        ha
        a
    

    Hint: add spaces to the beginning and end of the text string.