I am working on an assignment and I am trying to make a code for the below mentioned problem.
Write a python script that reads a types text, analyzes how many words the text consists of, and prints the total number of words as well as the number of "short" words of just three letter or less.
The given string is: "The play 's the thing wherein I'll catch the conscience of the king."
There is a small trick in the question. One cannot use the split() function because it will consider "I'll" as one word but the assignment requires us to consider it as two different words and hence giving an output that shows that the string has 14 words.
When it comes to the "short words". It should again consider "I'll" as two separate short words and it should give an output that shows that the string has 8 short words i.e. ["The", "s", "the", "I", "ll", "the", "of", "the"].
Thanks a lot and I would love it if you could share a code for this problem.
string= input("Enter string:")
word=1
y = 0
char = 0
for i in string:
if(i == ' ' or i == "'"):
word = word+1
for x in i:
if len(x) <= 3:
y = y+1
print("Number of words in the string:")
print(word)
print (y)
This is my code and the output is below:
Number of words in the string:
16
69