codingdirectional: Remove exclamation mark from a string with python

In this article, we will create a simple python program which will remove the exclamation mark from a string based on the second parameter which specifies how many exclamation marks should be removed from the beginning till the end of that string.

Below is that simple solution.

 def remove(s, n):          no_ex = list(s)     while(n > 0):         if('!' not in no_ex):             return ''.join(no_ex)         else:             no_ex.remove('!')         n -= 1     return ''.join(no_ex) 

If we enter (‘Hi!!’, 1000) into the above program, that method will only remove two ‘!’ because that is the total exclamation mark the string has. And if we enter (“Hi!!!!!”, 2) into the above method then the program will only remove 2 exclamation marks in the string and leaves the other exclamation mark untouched. If you have a better solution do leave your solution in the comment box below this post.

Planet Python

Leave a Reply

Your email address will not be published. Required fields are marked *