How to trim n characters from right end of a string?

Tony Meyer t-meyer at
Sun Jun 27 01:55:07 CEST 2004

> Please tell this newbie how to remove n characters from the 
> right end of a string.

You can't - Python strings are immutable.  You can, however, create a new
string that is the same as the old one less the n rightmost characters,
which should do what you need.

>>> s = "This is a string"
>>> s[:10]
'This is a '

The tutorial on strings would be well worth reading to help with this sort
of thing.

=Tony Meyer

More information about the Python-list mailing list