4
Contributors
4
Replies
11
Views
9 Years
Discussion Span
Last Post by jrcagle
0

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

0

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'
0
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
>>>

:(

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.