Re: How to delete a last character from a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fredrik Lundh

    #1

    Re: How to delete a last character from a string

    dudeja.rajat@gm ail.com wrote:
    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:
    explicitly comparing against true is bad style; better write that as

    if item.endswith(' \\'):
    item = item[0:-1] # This one I googled and
    found to remove the last character /
    dirListFinal.ap pend(item)
    else:
    dirListFinal.ap pend(item)
    >
    >
    item.endswith() does not seem to be working.
    item.endswith(" \\") works just fine:
    >>item = "name\\"
    >>print item
    name\
    >>print repr(item)
    'name\\'
    >>item.endswith ("\\")
    True
    >>if item.endswith(" \\") == True:
    .... print item[:-1]
    ....
    name

    instead of assuming that some builtin function is broken, try printing
    the the value of item to check that it really contains what you think it
    does. (use print repr(item) to see control characters etc).

    </F>

Working...