forked from mrayanasim09/python-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_count.py
31 lines (21 loc) · 835 Bytes
/
word_count.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# This code is made by MRayan Asim
import string
sentence = input("Enter the text: ")
def count_letters_in_sentence(sentence):
# Remove any punctuation marks and split the sentence into words
words = sentence.replace(",", "").replace(".", "").split()
# Initialize the letter count
count = 0
# Iterate over each word and count the letters
for word in words:
count += len(word)
# Return the total count of letters
return count
# Printing original string
print("The original string is: " + sentence)
# Using sum() + strip() + split() function
res = sum([i.strip(string.punctuation).isalpha() for i in sentence.split()])
# Number of words
letter_count = count_letters_in_sentence(sentence)
print("Number of letters:", letter_count)
print("The number of words in the string is:", str(res))