'How to remove "!" in a sentence except last?
I want to write a program that removes all exclamation points except those that are at the end of a sentence.
I have done this, to remove every exclamation point in a given sentence.
string = input("Enter a string with exclamation marks: ")
char = set('!')
if any((c in char) for c in string):
print (string.replace('!', ''))
So basically now, how I target the last that are after the last letter of a sentence?
Solution 1:[1]
If you have more than one exclamation mark at the end of the sentence, then you need to count them first and use their length to reduce the original length of the sentence, as follows:`
Sentence = "!!!one! one two two! two!!! three! ! three one!!!"
length=len(Sentence)
c=0
for i in Sentence[::-1]:
if(i=='!'):
c+=1
else:
break
Sentence=Sentence[:(length-c)].replace('!','')+Sentence[(length-c):length]
`
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
