Miguel 2488
Miguel 2488

Reputation: 1440

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.

Upvotes: 0

Views: 1830

Answers (2)

taras
taras

Reputation: 6915

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.

Upvotes: 1

GianAnge
GianAnge

Reputation: 633

Previous answers seem to be good, anyway I'll suggest to consider a regex approach. Just check, for each word, wether is present a punctuation character and if yes remove it.

Here an example:

import re

t = 'test.'
punctuations = '[!,\.:;"\']'
m = re.search( punctuations, t )
t.replace( m.group(0), '')
#out: 'test'

Upvotes: 1

Related Questions