Module strings
source code
Many operations on Python strings use the same syntax and operators as
lists. The Pythonic conception is that both belong to a 'super' data
type, sequences.
>>> X = 'dogs'
>>> Y = 'd'
There is an empty string, just as there is an empty list:
>>> Z = ''
>>> L = str(1)
L is no longer an int!
>>> L
'1'
>>> I = int(str(1))
>>> I
1
>>> M = str()
>>> M
''
>>> X[0]
'd'
>>> X[1]
'o'
>>> X[-1]
's'
>>> X[0:2]
'do'
>>> X[:-1]
'dog'
The following raises an IndexError
>>> X[4]
...
IndexError: list index out of range
>>> X + Y
'dogsd'
There is one thing that can be done with lists that canNOT be done
with strings. Assignment of values:
>>> 'spin'[2]= 'a'
...
TypeError: object does not support item assignment
More on this in basics.mutability.