How do I remove the last n characters from a string?

python

Solution

Two solutions here.

To remove the last 4 characters in general:

s = 'this is a string1234'

s = s[:-4]

yields

'this is a string'

And more specifically geared toward filenames, consider os.path.splitext() meant for splitting a filename into its base and extension:

import os 

s = "Forest.bmp"
base, ext = os.path.splitext(s)

results in:

print base
'Forest'

print ext
'.bmp'

Problem

If I have a string and want to remove the last 4 characters of it, how do I do that? So if I want to remove `.bmp` from `Forest.bmp` to make it just `Forest` How do I do that? Thanks.

Original source