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.
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'
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