Search code examples
pythonstringpython-3.xtextpunctuation

removing the last punctuation character


i know this question is susceptible to be marked as a duplicate, but after searching in the website i didn't find anything that matches what i really want to do.

I have a string like this :

string = "hey, that's you(this is a test)!"

i'm working on a function that removes only the last punctuation mark in any string, not punctuation embedded on a word, nor the leading punctuation signs,this function should also store the word frequencies in a dictionary.

This my code so far:

def word_counts(string):
    s = string.lower().split()
    dic = {}

    for key in string:
        key = "".join([l for l in key if l.isalpha()])

        if key in dic :
            dic[key] += 1

        else:
            dic[key] = 1


    return dic

my code is yelling the following results:

{'a': 1, 'hey': 1, 'is': 1, 'test': 1, 'thats': 1, 'youthis': 1}

but what i need is:

{'a': 1, 'hey': 1, 'is': 1, 'test)': 1, 'that's': 1, 'you': 1, (this': 1}

Note that in the word 'test)' the exclamation sign is removed but the parenthesis needs to stay. Any clues about how to do this??

Thank you all in advance

EDIT:

the comma after "hey" should leave. I should only remove one punctuation sign at a time, so if i find 2 at the end of a word, only one is to be removed.


Solution

  • What about checking for any possible punctuation mark and returning a stripped string when found one?

    import string
    
    def strip_last_punctuation(s):
        if s and s[-1] in string.punctuation:
            return s[:-1]
        else:
            return s
    
    test_str = "hey, that's you(this is a test)!"
    print(strip_last_punctuation(test_str))
    

    EDIT: Removed unnecessary for loop.