faqts : Computers : Programming : Languages : Python : Common Problems : Strings

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 7 people (100%) answered Yes
Recently 7 of 7 people (100%) answered Yes

Entry

Is there a function that deletes n chars from the left/right of a string?

Nov 11th, 2003 14:21
Jim Vanderveen, Sam G,


One way to do this is with slices.
>>> s = '1234567'
>>> print s
1234567
>>> # Strip off the rightmost 2 chars
>>> s = s[:-2]
>>> print s
12345
>>> # Strip off the leftmost 2 chars
>>> s = s[2:]
>>> print s
345