String operations

  1. Which are the values of variables a, b, c, d, l, e, f, g after the execution of these lines of code. Guess it manually and then check your answer using a Python shell.

    >>> s='abcdef'
    >>> a=s[3]
    >>> b=s[5]
    >>> c=s[0]
    >>> l=len(s)
    >>> d=s[l-1]
    >>> e=s[l-4]
    >>> f=s[-1]
    >>> g=s[-4]
    
  2. From the initialization of string variable s in the first line, evaluate the following expressions. Guess it manually and then check your answer using a Python shell.

    >>> s = 'daydreamer'
    >>> s[2]
    >>> s[-1]
    >>> s[:3]
    >>> s[-7:]
    >>> 'dream' in s
    >>> 'dreams' in s
    >>> s[-3::-5] + ' ' + s[1::3]