How to delete a last character from a string

D

dudeja.rajat

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
 
M

Mike Kent

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

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.
 
M

Mike Driscoll

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


Try something like this:
x = x[:-1]

This works with Python 2.5.2 on Windows XP.

Mike
 
D

dudeja.rajat

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


Try something like this:
x = x[:-1]

This works with Python 2.5.2 on Windows XP.

Mike


There is just a single \ at the end of every item. My list is as below:
['Results v1.0/', 'Results v1.1/']

so,

if x.endswith('\\'):

is that correct?
 
D

dudeja.rajat

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


Try something like this:
x = 'test\\'
if x.endswith('\\'):
x = x[:-1]

This works with Python 2.5.2 on Windows XP.

Mike


There is just a single \ at the end of every item. My list is as below:
['Results v1.0/', 'Results v1.1/']

so,

if x.endswith('\\'):

is that correct?

Sorry Guys. I did rubbish here and bothered you guys.
I did not recognize that I need to check for / character and not \

Really very sorry. Working for the last 14 hrs, could not see this
properly. Its time I must go home and take rest.


Regards,
Rajat
 
F

Fredrik Lundh

There is just a single \ at the end of every item. My list is as below:
['Results v1.0/', 'Results v1.1/']

so,

if x.endswith('\\'):

is that correct?

if your list contains forward slashes, maybe you should test for forward
slashes and not backward slashes.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top