| | |
remove a letter in a string
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2007
Posts: 1
Reputation:
Solved Threads: 0
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
...
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
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
Do you want to remove it by by index (3) or value ('n')?
To remove by index, you could do
which will give good results even if len(s) < 4.
If you want to remove by value, you could do
(which will clobber all 'n's)
Jeff
To remove by index, you could do
Python Syntax (Toggle Plain Text)
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)
s = s.replace('n','')
(which will clobber all 'n's)
Jeff
•
•
Join Date: Apr 2006
Posts: 150
Reputation:
Solved Threads: 40
Python Syntax (Toggle Plain Text)
s = "open sesame" print s[0:s.index('n')] + s[ s.index('n') +1 : ]
If you just want to remove the first 'n' use:
python Syntax (Toggle Plain Text)
old = "open sesame nnnn" # replace first occurance of 'n' new = old.replace('n', '', 1) print new # 'ope sesame nnnn'
drink her pretty
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
•
•
•
•
Python Syntax (Toggle Plain Text)
s = "open sesame" print s[0:s.index('n')] + s[ s.index('n') +1 : ]
Python Syntax (Toggle Plain Text)
>>> 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 >>>
![]() |
Similar Threads
- how to remove space from string? (PHP)
- How to remove spaces from a string (Shell Scripting)
- How to Remove Newline character from a VB String (Visual Basic 4 / 5 / 6)
- removing the null terminator at the end of string (C)
- How to count Uppercase Letter from a String (ASP)
Other Threads in the Python Forum
- Previous Thread: im totaly new for programing, need some help
- Next Thread: help with using html in browsers
Views: 18082 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Python
advanced anydbm app bash beginner bits calling chmod cmd code data dictionary directory dynamic edit examples excel external feet file float format ftp function gui halp homework http images import info input ip itunes java keycontrol line linux list lists loan loop maintain millimeter mouse newb number numbers output panel parsing path port prime print program programming projects push py-mailer py2exe pygame pyqt python queue random recursion recursive scrolledtext smtp split ssh string strings sudokusolver table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode update urllib urllib2 variable variables ventrilo web webservice whileloop windows wxpython xlib






