Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 7th, 2004, 01:00 PM
lblack750 lblack750 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 1 lblack750 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
removing unwanted characters from a string

I was wondering how to remove a character from a string that was retreived from a text file (.txt).

Reply With Quote
  #2  
Old May 7th, 2004, 01:37 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,585 DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
If you only want to remove all instances of a single character, then you can use the string replace method:

Code:
>>> txt = 'hello world'
>>> txt.replace('l', '')
'heo word'


To remove a set of characters then you could use the filter function:

Code:
>>> filter(lambda x: x not in 'aeiou', txt)
'hll wrld'


Alternatively you could use the string module's translate and maketrans functions:

Code:
>>> import string
>>> trans = string.maketrans('', '')
>>> string.translate(txt, trans, 'aeiou')
'hll wrld'


The translate function is more useful if you wish to replace some characters and remove others.
EDIT: translate is also much faster than filter, since it is implemented entirely in C, while filter will need to call a python function for each character. This probably does not matter much for scripts occasionally acting on short strings, but can be critical if performance is an issue.

Dave - The Developers' Coach

Last edited by DevCoach : May 8th, 2004 at 05:04 PM.

Reply With Quote
  #3  
Old May 8th, 2004, 03:06 PM
sfb sfb is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Posts: 623 sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 5 h 53 m 21 sec
Reputation Power: 33
It all depends which characters you want to remove...

To remove whitespace characters (space, tabs, newlines, etc) from the left end of the string:

Code:
>>> txt = "  Hello World  "
>>> txt.lstrip()
"Hello World  "


From the right of the string:

Code:
>>> txt = "  Hello World  "
>>> txt.rstrip()
"  Hello World"


From both sides:

Code:
>>> txt = "  Hello World  "
>>> txt.strip()
"Hello World"


To remove the first character:

Code:
>>> txt = "Hello World"
>>> txt[1:]
"ello World"


The last character:

Code:
>>> txt = "Hello World"
>>> txt[:-1]
"Hello Worl"


The 5th character (or the nth character(s)):

Code:
>>> txt = "Hello World"
>>> txt[:5] + t[6:]
"HelloWorld"



Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > removing unwanted characters from a string

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap