Newbie asks, how to get rid of the last four characters of a string?

Ben Caradoc-Davies ben at wintersun.org
Wed Jan 22 09:37:56 EST 2003


On Wed, 22 Jan 2003 15:22:47 +0100, Christopher Culver 
<kricxjo.neniuspamajxo at yahoo.com> wrote:
> I'm somewhat new to Python and have run into this problem: I have a script
> whose first argument takes a MS Word file, which would end with .doc. I
> want to remove ".doc" and have just the file name without extension. Now,
> I know I can do this with regular expressions, but is there a function
> that can be used to chomp off the last four characters without having to
> import the re module?

For the sequence a, the slice a[:-n] gives a without the last n elements. A
string is a type of sequence. For example,

"filename.doc"[:-4]

is just

"filename"

-- 
Ben Caradoc-Davies <ben at wintersun.org>
http://wintersun.org/
Imprisonment on arrival is the authentic Australian immigration experience.




More information about the Python-list mailing list