b = a[:3]
will take 3 firsts characters of the string
b = a[:15]
will not make an error (not sure on python2)
c = a[-3:]
for the last 3 characters
But you still can override (inherit) string type
class MySuperString(str):
def left(self, num_char):
return self[:num_char]
a = MySuperString('plop')
b = a.left(3)
see for example : http://www.nomadiccodemonkey.com/?p=590
edit :
if isinstance(a, str):
won't work anymore, use this instead :
if issubclass(a, str):
or naturally this :
if isinstance(a, MySuperString):
french joke : et ça marche même si tu mets des SuperSlip !