How to delete a ast character from a string?

Chris Rebert cvrebert+clp at gmail.com
Fri Aug 29 20:40:15 CEST 2008


On Fri, Aug 29, 2008 at 11:25 AM,  <dudeja.rajat at gmail.com> wrote:
> Hi,
>
> I've a list some of whose elements with character \.
> I want to delete this last character from the elements that have this
> character set at their end,
>
> I have written a small program, unfortunately this does not work:
>
> dirListFinal = []
> for item in dirList:
>            print item
>            if item.endswith('\\') == True:

You san simplify that line to just:
            if item.endswith('\\'):

>                item = item[0:-1]         # This one I googled and
> found to remove the last character /

And you don't need the leading 0, so just use:
                item = item[:-1]

>                dirListFinal.append(item)
>            else:
>                dirListFinal.append(item)

And those last 3 lines are a bit redundant. Just put one

  dirListFinal.append(item)

at the same indentation level as the "if" and delete those 3 lines.

Not that these changes will necessarily fix your program, but they do
make it easier to comprehend for the reader.

- Chris

>
>
> item.endswith() does not seem to be working.
>
> Please help
> --
> Regrads,
> Rajat
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list