473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Re: How to delete a last character from a string

du**********@gmail.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.append(item)
else:
dirListFinal.append(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>

Aug 29 '08 #1
0 1121

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Olivia Towery | last post by:
How do I delete the last character in a string? Thanks, Olivia Towery
6
by: Carol | last post by:
With ASP, I am generating a huge CSV file. It is to large to open in notepad. Can I use the Filesystem Scripting Object to read the very last line of the file and delete the very last...
3
by: ozbear | last post by:
The title says it all. Since a Richedit control doesn't understand a backspace character ('\b', or 0x08) when I receive one I need to programmatically delete the last character from the Richedit...
5
by: michael | last post by:
Hi All, I have the following: const int LENGTH = 5; void limitNameLength(string inText, char *&outText, int outLength){ if(static_cast<int>(inText.length()) outLength){ inText =...
7
by: ITAutobot25 | last post by:
My delete button is not working in my GUI and my due date is today before midnight. Can anyone show me how to correct this error? My assignment statement is below as well as 5 classes. InventoryGUI...
2
by: Joca | last post by:
This is probably something easy to do, but i'm trying to erase the last character from a string, for example: "Cats" into "Cat" Does anyone have any idea on how to do that? Thanks in advance.
0
by: dudeja.rajat | last post by:
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,...
5
by: dudeja.rajat | last post by:
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...
0
by: dudeja.rajat | last post by:
On Fri, Aug 29, 2008 at 7:40 PM, Chris Rebert <cvrebert+clp@gmail.comwrote: Thanks for the suggestions. I wondered if the item is really a string. So I added the following to check this: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.