In [1]:
mystring = 'Hello World'

In [2]:
mystring


Out[2]:
'Hello World'

In [3]:
mystring[0]


Out[3]:
'H'

In [4]:
mystring[8]


Out[4]:
'r'

In [6]:
mystring[-2]


Out[6]:
'l'

In [7]:
mystring[9]


Out[7]:
'l'

example of positive and negative indexing.


In [9]:
mystring[2:5:1]


Out[9]:
'llo'

Start:Stop:Step


In [10]:
mystring[0:4:2]


Out[10]:
'Hl'

In [11]:
mystring[0:mystring:2]


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-a34e4c9565ed> in <module>()
----> 1 mystring[0:mystring:2]

TypeError: slice indices must be integers or None or have an __index__ method

In [12]:
mystring[0:11:2]


Out[12]:
'HloWrd'

In [13]:
mystring[0:11:3]


Out[13]:
'HlWl'

In [14]:
mystring[0:5:1]


Out[14]:
'Hello'

In [17]:
mystring[0:12:6]


Out[17]:
'HW'

In [18]:
mystring[0:12:7]


Out[18]:
'Ho'

In [19]:
mystring[6:13:6]


Out[19]:
'W'

In [22]:
mystring[5:13:7]


Out[22]:
' '

In [23]:
mystring[::1]


Out[23]:
'Hello World'

In [24]:
mystring = 'Hello World this is used as another example of no start stop'

In [25]:
mystring


Out[25]:
'Hello World this is used as another example of no start stop'

In [26]:
mystring[::1]


Out[26]:
'Hello World this is used as another example of no start stop'

In [27]:
mystring[::-1]


Out[27]:
'pots trats on fo elpmaxe rehtona sa desu si siht dlroW olleH'

Trick reverse string exmaple


In [ ]: