I'm new to python and I'm trying to count punctuation in a string input by user, and I"ve been able to do it w/ out a loop, but would like to emply a more efficiant method.
I've been able to loop through and grabb the characters, but they are'nt being displayed as a single value which is what I need so I can add this snipit to a larger piece of code.

here is my code:

str1 = input("Please enter a sentence:  ")
    
     
    for i in ["!","."]:
        g = str1.count(i)
        print(g)

Recommended Answers

This perhaps print(sum(str1.count(c) for c in ".!")) or sum(c in ".!" for c in str1)

Jump to Post

All 2 Replies

This perhaps print(sum(str1.count(c) for c in ".!")) or sum(c in ".!" for c in str1)

thanks keemosabee, works like "ballz marie"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.