Skip to main content

Featured

Write a Python Program to find palindrome of a given string

If you reverse the word then you get the same word that word is palindrome. def   palindrome ( str ):     pointer =  str       rev_str =  reversed (pointer)       if   list (pointer) ==  list (rev_str):         print ( "It is palindrome" )       else :         print ( "It is not palindrome" )  palindrome( 'level' ) palindrome( 'madam' ) palindrome( 'life' ) palindrome( 'refer' ) Output:  It is palindrome It is palindrome It is not palindrome It is palindrome

Word count in python from file

 In this post lets create a illustrative program to count the words present in the given file

This can be done using module and without module. Lets create without module

Program:

FileName = 'newfile.txt'

Count_Dict = {}

f1 = open(FileName,'r')

for line in f1:
    word_list = line.split()
    for word in word_list:
        if word not in Count_Dict:

            Count_Dict[word] = 1
        else:

             Count_Dict[word] = Count_Dict[word]+1

print("count of each word in file:")

print('{:15}{:3}'.format('word','Frequency'))

print('-'*25)

for (word,count) in Count_Dict.items():

     print('{:15}{:3}'.format(word,count))

Output:


count of each word in file: word Frequency ------------------------- Hello 3 "Hi" 3 "Hey" 3 Good 3 Bye 3

Try files with more and different words !!

Feel free to comment your thoughts!!!!


Comments

Popular Posts