Simple Game Word Count with Regex in Python

Bagustyo
1 min readMay 4, 2020

“ simple note learning python for beginner”

Well, since yesterday my niece ask me how to make some simple programming to counting word from text, I talk to her “ Honey you can use word and or Excel ” but she insist trough with programming , and as cool uncle I said “ Ok , let try to bake it”.

import reword_dic2 = {}with open('small.txt','r') as baca:
baca = baca.read()
print(baca)

cari = re.findall(r'\b[a-zA-Z]{1,20}\b',baca)
for c in cari:
if str(c) in word_dic2:
word_dic2[str(c)] +=1
else:
word_dic2[str(c)] = 1
print(word_dic2)

she look happy, but next question coming , what if I want to count another file? , for now you can change ‘small.txt’ into another text what you want, next step I’ll teach you function , but next time

We are not what we should be
We are not what we need to be
But at least we are not what we used to be
-- Football Coach


{'We': 2, 'are': 3, 'not': 3, 'what': 3, 'we': 4, 'should': 1, 'be': 3, 'need': 1, 'to': 2, 'But': 1, 'at': 1, 'least': 1, 'used': 1, 'Football': 1, 'Coach': 1}

--

--

Bagustyo
0 Followers

be better and always be good