views:

172

answers:

4

I want to remove the first characters from a string. Is there a function that works like this?

>>> a = "BarackObama"
>>> print myfunction(4,a)
ckObama
>>> b = "The world is mine"
>>> print myfunction(6,b)
rld is mine
+4  A: 

Use slicing.

>>> a = "BarackObama"
>>> a[4:]
'ckObama'
>>> b = "The world is mine"
>>> b[6:10]
'rld '
>>> b[:9]
'The world'

You can read about this and most other language features in the official tutorial: http://docs.python.org/tut/

Mike Graham
+6  A: 

Yes, just use slices:

 >> a = "BarackObama"
 >> a[4:]
 'ckObama'

Documentation is here http://docs.python.org/tutorial/introduction.html#strings

dragoon
Short, succinct and clear. With a reference directly to String section. Mine is just a poor rehash. I learned some Python to answer this question... and I shall now remove my answer. Must cut the clutter!
Atømix
+2  A: 

The function could be:

def cutit(s,n):    
   return s[n:]

and then you call it like this:

name = "MyFullName"

print cutit(name, 2)   # prints "FullName"
joaquin
+1  A: 
a = 'BarackObama'
a[4:]  # ckObama
b = 'The world is mine'
b[6:]  # rld is mine
Alan Haggai Alavi