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:?:

Recommended Answers

All 4 Replies

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

To remove by index, you could do

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

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

(which will clobber all 'n's)

Jeff

s = "open sesame"
print s[0:s.index('n')] + s[ s.index('n') +1 : ]

If you just want to remove the first 'n' use:

old = "open sesame nnnn"
# replace first occurance of 'n'
new = old.replace('n', '', 1)
print new  # 'ope sesame nnnn'
s = "open sesame"
print s[0:s.index('n')] + s[ s.index('n') +1 : ]
>>> s='fred'
>>> print s[0:s.index('n')] + s[ s.index('n') +1 : ]

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in -toplevel-
    print s[0:s.index('n')] + s[ s.index('n') +1 : ]
ValueError: substring not found
>>>

:(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.