How to delete a last character from a string

Mike Kent mrmakent at cox.net
Fri Aug 29 14:41:29 EDT 2008


On Aug 29, 2:28 pm, dudeja.ra... at gmail.com wrote:
> Sorry : Earlier mail had a typo in Subject line which might look
> in-appropriate to my friends
>
> 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:
>                item = item[0:-1]         # This one I googled and
> found to remove the last character /
>                dirListFinal.append(item)
>            else:
>                dirListFinal.append(item)
>
> item.endswith() does not seem to be working.
>
> Please help
> --
> Regrads,
> Rajat

Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = "hello\\"
>>> s.endswith("\\")
True
>>> s[:-1]
'hello'

I hate to say "works for me", but it works for me.  Perhaps you should
put some debugging statements in your code to see if that data you are
working on is what you are expecting.



More information about the Python-list mailing list