remove a letter in a string

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 1
Reputation: the_python is an unknown quantity at this point 
Solved Threads: 0
the_python the_python is offline Offline
Newbie Poster

remove a letter in a string

 
0
  #1
Mar 8th, 2007
im making this game that is called PyTanks. If you want to know more about it please visit http://pythongaming.py.funpic.org/pythonmadegames.html
...

Anyway,

I ran across this problem, how do you delet or remove a letter from a string. Lets say that my string is-

string = "open sesame"

I want to remove the "n" in the string above. (letter 3 in string)
how do i do this
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: remove a letter in a string

 
0
  #2
Mar 8th, 2007
Do you want to remove it by by index (3) or value ('n')?

To remove by index, you could do

  1. s = s[:3] + s[4:]

which will give good results even if len(s) < 4.

If you want to remove by value, you could do

  1. s = s.replace('n','')

(which will clobber all 'n's)

Jeff
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 150
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: remove a letter in a string

 
0
  #3
Mar 9th, 2007
  1. s = "open sesame"
  2. print s[0:s.index('n')] + s[ s.index('n') +1 : ]
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: remove a letter in a string

 
0
  #4
Mar 9th, 2007
If you just want to remove the first 'n' use:
  1. old = "open sesame nnnn"
  2. # replace first occurance of 'n'
  3. new = old.replace('n', '', 1)
  4. print new # 'ope sesame nnnn'
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: remove a letter in a string

 
0
  #5
Mar 9th, 2007
Originally Posted by ghostdog74 View Post
  1. s = "open sesame"
  2. print s[0:s.index('n')] + s[ s.index('n') +1 : ]
  1. >>> s='fred'
  2. >>> print s[0:s.index('n')] + s[ s.index('n') +1 : ]
  3.  
  4. Traceback (most recent call last):
  5. File "<pyshell#1>", line 1, in -toplevel-
  6. print s[0:s.index('n')] + s[ s.index('n') +1 : ]
  7. ValueError: substring not found
  8. >>>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 18082 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC