Python 3 Script to Count the Number of Times a Letter Appears in a Text File
5K views
Jun 1, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys welcome to this video So
0:02
in this video I will show you a very
0:04
simple Python utility kind of a script
0:06
which will actually specify how many
0:09
occurrences of each word is there inside
0:12
a text file So now let's suppose if you
0:14
have a text file where you have a number
0:17
of sentences and uh if you want to get
0:20
the total number of occurrences of a
0:22
particular word So now let let me create
0:24
a filet txt and uh let's suppose I just
0:28
do hello my name is gotham I just write
0:31
some basic
0:36
sentences so it can contain a lot of
0:38
text as well but I'm just giving a very
0:41
simple example I created this filet txt
0:44
I defined some basic sentences right
0:46
here and then this Python script will
0:49
automatically give the occurrences of
0:51
this So you can put any word here I put
0:55
G here So you just need to give the file
0:58
name file.txt If I run this Python
1:01
script it will give the occurrences of
1:03
uh the G letter which is repeated inside
1:07
this So it is saying G letter is only
1:09
repeated one time As you can see G is
1:12
appeared only one time You can put any
1:15
word here Let's suppose a common word I
1:17
put here which is
1:19
uh a word So I want to get the
1:22
occurrences of a
1:31
word
1:35
Sorry here we need to replace it which
1:38
is
1:40
a so I
1:44
simply run this Python script So it will
1:46
give a is repeated nine times inside
1:49
this file here File.tx txt It's a very
1:52
simple script but at the same times it's
1:54
very easy to do Let me show you the
1:57
Python script which is there So once we
1:59
define a simple function here which is
2:02
letter frequency So we want to get the
2:05
frequency of each word inside this
2:08
file.txt So we can say a Now we define
2:12
this function which is very simple which
2:16
is letter
2:20
frequency We put the file name and the
2:24
word and then we simply first of all for
2:27
doing this we read the file here in the
2:30
by using the open function we pass the
2:32
file name in the read mode read flag
2:36
After that we read the file here by the
2:39
read
2:40
function like this After that we simply
2:44
return the
2:46
content the text
2:49
dot count of that specific letter that
2:52
the user has passed So it contains a
2:55
readymate function which is count
2:57
function It will count the occurrences
2:59
of this word that you passed inside this
3:01
text file and then it will return So
3:04
again if you run this it will return
3:05
nine times you'll see that So it's a
3:08
very simple script Uh thank you very
3:10
much for watching this video and also
3:13
check out my website freemediattools.com
3:17
uh which contains thousands of tools
#Scripting Languages