Python program to count the occurrence of a given character in a String

In this tutorial, we will learn to count the occurrence of a given character in a string in Python. We will use python dictionaries and try to solve some problems based on strings. We will find the distinct characters, discrete characters, unique characters of the string and also count the number of words in the string.

Count occurrence of a given character in a String in Python

string = 'Python Programming'
dictionary = {}
for char in string:
    if( char in dictionary.keys()):
        dictionary[char] += 1
    else:
        dictionary[char]=1
for char in dictionary:
    print(char,' -> ',dictionary[char])

Output:

P -> 2
y -> 1
t -> 1
h -> 1
o -> 2
n -> 2
  -> 1
r -> 2
g -> 2
a -> 1
m -> 2
i -> 1
  • Consider a string (example: ‘Python Programming’) and create an empty dictionary.
  • Use the characters in the string as the keys and their count as values.
  • For every character in the string, if a character is present in the dictionary as a key, increment its corresponding value. Else add the new character to the dictionary as a key and assign its count value as one.

So now, we have created a python dictionary containing information about the string. Let’s see what we can do using this information.

  1. Duplicate characters of the String.
  2. Distinct characters of the string.
  3. Unique characters of the String.
  4. The number of words in the string.

Print all the duplicate characters of the String

string = 'Python Programming'
dictionary = {}
for char in string:
    if( char in dictionary.keys()):
        dictionary[char] += 1
    else:
        dictionary[char]=1
        
duplicates = []

for char in dictionary:
    if( dictionary[char] > 1 ):
       duplicates.append(char)
print(duplicates)

Output:

['P', 'o', 'n', 'r', 'g', 'm']
  • This program prints the list of repeated characters in the string.
  • If the count value of a character is more than 1 in the dictionary, the character is added to the list of duplicates.

Print all the distinct characters of the string

string = 'Python Programming'
dictionary = {}
for char in string:
    if( char in dictionary.keys()):
        dictionary[char] += 1
    else:
        dictionary[char]=1
        
distinct_char = []

for char in dictionary:
    if( dictionary[char] == 1 ):
       distinct_char.append(char)
       
print(distinct_char)

Output:

['y', 't', 'h', ' ', 'a', 'i']
  • This program prints the list of all the characters in the string which are distinct( characters not repeated).
  • If the count value of the character is one, we can consider it as distinct and add to the ‘distinct_char’ list.

Print all the unique characters of the String

string = 'Python Programming'
dictionary = {}
for char in string:
    if( char in dictionary.keys()):
        dictionary[char] += 1
    else:
        dictionary[char]=1

charecters = []

for char in dictionary:
    charecters.append(char)

print(charecters)

Output:

['P', 'y', 't', 'h', 'o', 'n', ' ', 'r', 'g', 'a', 'm', 'i']
  • This program prints the list of all unique characters present in the string.
  • List of all the characters in the string where the repeated characters are removed.

Count number of words in the string

string = 'Python Programming'
dictionary = {}
for char in string:
    if( char in dictionary.keys()):
        dictionary[char] += 1
    else:
        dictionary[char]=1
        
if(' ' in dictionary.keys()):
    print(dictionary[' ']+1, 'words')
else:
    print('1 word')

Output:

2 words

  • This program prints the number of words present in the string.
  • We know that there will be a space between two words. As a result, we can consider that for N spaces, there will be N+1 words.

Here is how the complete code should look like

string = 'Python Programming'
print('String: ',string)
dictionary = {}
for char in string:
    if( char in dictionary.keys()):
        dictionary[char] += 1
    else:
        dictionary[char]=1
        
duplicates = []
for char in dictionary:
    if( dictionary[char] > 1 ):
        duplicates.append(char)
print('Duplicates:', duplicates)

distinct_char = []
for char in dictionary:
    if( dictionary[char] == 1 ):
        distinct_char.append(char)
print('Distinct characters: ',distinct_char)

characters = []
for char in dictionary:
    characters.append(char)
print('Unique characters: ',characters)

if(' ' in dictionary.keys()):
    print('Number of words: ',dictionary[' ']+1)
else:
    print('Number of words: 1')

Output:

String: Python Programming
Duplicates: ['P', 'o', 'n', 'r', 'g', 'm']
Distinct characters: ['y', 't', 'h', ' ', 'a', 'i']
Unique characters: ['P', 'y', 't', 'h', 'o', 'n', ' ', 'r', 'g', 'a', 'm', 'i']
Number of words: 2

I hope you have understood the code…😊
If you have any quires, please feel free to drop in your comments.

Also, read Reverse a Python list using range().

Thank you..!

Leave a Reply