-2

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
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • I saw a similar question today. Maybe the answer there will help you: https://stackoverflow.com/questions/53691682/python-code-to-find-a-total-number-of-shortest-words-in-a-string – Virginia Dec 09 '18 at 13:35
  • Use regex like suggested in this question https://stackoverflow.com/questions/1059559/split-strings-with-multiple-delimiters and after that loop the list for checking the susbtring with length less than 4. – Iulian Dec 09 '18 at 13:38
  • Ah such a pity, I hoped that would help you. Good luck with your question though and you're welcome. – Virginia Dec 09 '18 at 13:41
  • Possible duplicate of [Python code to find a total number of shortest words in a string](https://stackoverflow.com/questions/53691682/python-code-to-find-a-total-number-of-shortest-words-in-a-string) – David Zemens Dec 09 '18 at 13:42
  • The other question *does* answer this question. It is the same question, probably from the same homework assignment. – David Zemens Dec 09 '18 at 13:42

5 Answers5

1

You can use re.split() to split on multiple delimiters:

import re

s = "The play 's the thing wherein I'll catch the conscience of the king"

lst = re.split(r"'| ", s)

all_words_lst = [x for x in lst if x]
print(f'Total words count: {len(all_words_lst)}')

short_words_lst = [x for x in lst if x and len(x) <= 3]
print(f'Total short words count: {len(short_words_lst)}')

# Total words count: 14
# Total short words count: 8
Austin
  • 25,759
  • 4
  • 25
  • 48
0

You can first replace "'" with " " and then later call split on resultant string.

>>> s = "The play's the thing wherein I'll catch the conscience of the king."
>>> s = s.replace("'", " ")
>>> s = s.split()
>>> len(s)
14
>>> s
['The', 'play', 's', 'the', 'thing', 'wherein', 'I', 'll', 'catch', 'the', 'conscience', 'of', 'the', 'king.']
lightyagami1
  • 105
  • 3
  • 7
0
x = "The play 's the thing wherein I'll catch the conscience of the king."
x = x.replace("'", " ")
x = x.split()
# Find all the words that have length less than 3
ans = [i for i in x if len(i) <= 3]
print("Total number of words {}, short words{}".format(len(x), len(ans)))
0

With re.split function:

import re

input_string = input("Enter string:")  # for ex. "He is a good-hearted person, too"
words = re.findall(r"\w+(?:-\w+)?", input_string)

print("Total number of words:", len(words))
print("Number of 'short' words:", len([w for w in words if len(w) <= 3]))

The output:

Total number of words: 6
Number of 'short' words: 4
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • It is working perfectly for this string but if I try to change the string then, it shows the wrong output. for example let us take the following string "Our previous trip was good, too." It does not count "too" as a short word. –  Dec 09 '18 at 14:02
  • Oh great its a great solution but now it excludes one sort of strings. What if I have the string that includes "-". For eg, If there is a string like "He is a good-hearted person, too." In this case, the code should take "good-hearted" as one word instead of two. Is there a way to do so? –  Dec 09 '18 at 14:22
0

You can change all characters ' to space. Then split() without any arguments returns the list of all words.

string= input("Enter string:")
word=0
y = 0

for i in range(len(string)):
    if string[i] == "\'":
        string[i] = ' '
for i in string.split():
    word += 1
    if len(i) <= 3:
        y += 1

print("Number of words in the string:")
print(word)
print (y)
Kuba
  • 123
  • 6