944,098 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 55776
  • Python RSS
Mar 8th, 2007
0

remove a letter in a string

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
the_python is offline Offline
1 posts
since Mar 2007
Mar 8th, 2007
0

Re: remove a letter in a string

Do you want to remove it by by index (3) or value ('n')?

To remove by index, you could do

Python Syntax (Toggle Plain Text)
  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

Python Syntax (Toggle Plain Text)
  1. s = s.replace('n','')

(which will clobber all 'n's)

Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Mar 9th, 2007
0

Re: remove a letter in a string

Python Syntax (Toggle Plain Text)
  1. s = "open sesame"
  2. print s[0:s.index('n')] + s[ s.index('n') +1 : ]
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Mar 9th, 2007
0

Re: remove a letter in a string

If you just want to remove the first 'n' use:
python Syntax (Toggle Plain Text)
  1. old = "open sesame nnnn"
  2. # replace first occurance of 'n'
  3. new = old.replace('n', '', 1)
  4. print new # 'ope sesame nnnn'
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Mar 9th, 2007
0

Re: remove a letter in a string

Click to Expand / Collapse  Quote originally posted by ghostdog74 ...
Python Syntax (Toggle Plain Text)
  1. s = "open sesame"
  2. print s[0:s.index('n')] + s[ s.index('n') +1 : ]
Python Syntax (Toggle Plain Text)
  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. >>>
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Python Forum Timeline: im totaly new for programing, need some help
Next Thread in Python Forum Timeline: help with using html in browsers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC