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
You can use Python's regular expressions to remove the first n characters from a string, using re's . sub() method. This is accomplished by passing in a wildcard character and limiting the substitution to a single substitution.
Yes, just use slices:
>> a = "BarackObama"
>> a[4:]
'ckObama'
Documentation is here http://docs.python.org/tutorial/introduction.html#strings
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With