Hi I guess that the title says it all.
For example if I have a string like: "The food! is great!!!"
I want python to change it to: "The food is great!"
Thank you all in advance. :)
Hi I guess that the title says it all.
For example if I have a string like: "The food! is great!!!"
I want python to change it to: "The food is great!"
Thank you all in advance. :)
You can use a regex with lookahead:
import re
re.sub(r'!+(?=.*\!)','',text)
The regex
!+(?=.*\!)
matches any sequences of exclamation marks given the lookahead (?=.*\!)
sees an exclamation mark. All these exclamation marks are sub
stituted by the empty string.
$ python2
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> text="The food! is great!!!"
>>> re.sub(r'!+(?=.*\!)','',text)
'The food is great!'
strs="The food! is great!!!"
count=strs.count("!")-1
strs = strs.replace('!','',count)
print strs
https://stackoverflow.com/a/16268024/6626530
output
The food is great!
replace all '!' in the string with ''. then string = string + '!'
string.replace('!','')
string = string + '!'