deleting the first and the last character of a string

Manuel Hendel python-list at
Wed Sep 11 06:58:18 EDT 2002

I got a file with fields seperated by a "|". The problem is that there
is also a "|" at the beginning and at the end of each line although
there no fields in front or behind these "|". Now I want to delete
these two "|". Is there anything which says delete the first or last
character of a string? I checked the string module on the web, but I
couldn't find anything.

I solved it like this, but I honestly don't like it that way.

#!/usr/bin/env python
import sys
import string

inputfile = open(sys.argv[-1], "r")

for line in inputfile.readlines():
        fields = string.split(line, "|")
        lineN = string.join(fields[1:-1], "|")

Isn't there any better solution? ;-)


The real purpose of books is to trap the mind into doing its own thinking. 
-Christopher Morley 

More information about the Python-list mailing list