CodeTestValidate

No installations needed! Write Python directly in your browser and get instant feedback with our smart validation system.

Challenge # 5

Count Words in a Sentence (Ignore Punctuation)

Objective

Parse a sentence, remove punctuation, and count the number of words.

Instructions

  • You are given a string variable named sentence.

  • Write a function count_words(sentence) that returns the number of words in the sentence.

  • You must ignore punctuation characters such as . , ! ? ; : and count only valid words.

  • Words are separated by spaces.

  • Treat contractions like "don't" as one word.

  • Assume the input is a valid string.

Constraints:
Do not use input(). • Use only standard Python libraries.
Exemple IO

Input: sentence = "Hello, world!"

Output: 2

Input: sentence = "Well... this is unexpected, isn't it?"

Output: 6

Input: sentence = "No punctuation here"

Output: 3

Try It Online!

Output:

Validate Your Results

Get your code reviewed and validated to understand what you did right and where you can improve.

Previous challengeFind the Second-Largest NumberNext challengeImplement Stack and Queue Using Lists
Loading...